Skip to content

Commit a5ada57

Browse files
committed
fix(toolbar): update example readmes, add htaccess to fix wp-env issues
1 parent 712bf6a commit a5ada57

File tree

8 files changed

+76
-116
lines changed

8 files changed

+76
-116
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,5 @@ __MACOSX
3434
examples/**/package-lock.json
3535
examples/**/__MACOSX
3636
examples/**/mu-plugin.php
37-
examples/**/.htaccess
3837
examples/**/.wp-env.json
3938
``
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# BEGIN WordPress
2+
<IfModule mod_rewrite.c>
3+
RewriteEngine On
4+
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
5+
RewriteBase /
6+
RewriteRule ^index\.php$ - [L]
7+
RewriteCond %{REQUEST_FILENAME} !-f
8+
RewriteCond %{REQUEST_FILENAME} !-d
9+
RewriteRule . /index.php [L]
10+
</IfModule>
11+
# END WordPress

packages/toolbar/examples/next/.gitignore

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/toolbar/examples/next/.wp-env.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"phpVersion": "8.3",
2+
"phpVersion": "8.4",
33
"plugins": [
44
"https://github.com/wp-graphql/wp-graphql/releases/latest/download/wp-graphql.zip",
55
"../plugins/hwp-cors-local",
@@ -17,10 +17,9 @@
1717
"HEADLESS_FRONTEND_URL": "http://localhost:3000"
1818
},
1919
"mappings": {
20-
"db": "./wp-env/db",
21-
".htaccess": "./.htaccess"
20+
".htaccess": "./wp-env/setup/.htaccess"
2221
},
2322
"lifecycleScripts": {
24-
"afterStart": "wp-env run cli -- wp theme activate twentytwentyfour && wp-env run cli -- wp rewrite structure '/%postname%/' --hard"
23+
"afterStart": "wp-env run cli -- wp theme activate twentytwentyfour && wp-env run cli -- wp rewrite structure '/%postname%/' && wp-env run cli -- wp rewrite flush"
2524
}
26-
}
25+
}

packages/toolbar/examples/next/README.md

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ In this example we show how to integrate the Headless WordPress Toolbar into a N
44

55
## Table of Contents
66

7-
8-
97
## Getting Started
108

119
> [!IMPORTANT]
1210
> Docker Desktop needs to be installed to run WordPress locally.
1311
1412
1. Create a `.env.local` file in the `examples/next/toolbar-demo` directory with the following content:
15-
```env
13+
14+
```env
1615
NEXT_PUBLIC_WP_URL=http://localhost:8888
17-
```
16+
```
17+
1818
2. Run `npm run example:setup` to install dependencies and configure the local WP server.
1919
3. Run `npm run example:start` to start the WP server and Next.js development server.
2020

21-
22-
>[!NOTE]
21+
> [!NOTE]
2322
> Port 8888 is the default port for wp-env.
2423
2524
The example will be available at:
25+
2626
- **Frontend**: http://localhost:3000
2727
- **WordPress**: http://localhost:8888
2828
- **WordPress Admin**: http://localhost:8888/wp-admin (`admin` / `password`)
@@ -36,37 +36,33 @@ The example will be available at:
3636
### 1. React Hooks Integration
3737

3838
```tsx
39-
import { toolbar } from '@/lib/toolbar';
40-
import { useToolbar } from '@wpengine/hwp-toolbar/react';
39+
import { toolbar } from "@/lib/toolbar";
40+
import { useToolbar } from "@wpengine/hwp-toolbar/react";
4141

4242
function MyComponent() {
4343
const { state, nodes } = useToolbar(toolbar);
4444

45-
return (
46-
<div>
47-
{/* Toolbar UI */}
48-
</div>
49-
);
45+
return <div>{/* Toolbar UI */}</div>;
5046
}
5147
```
5248

5349
### 2. WordPress Context Integration
5450

5551
```tsx
56-
import { fetchWordPressUser } from '@/lib/wordpress';
52+
import { fetchWordPressUser } from "@/lib/wordpress";
5753

5854
// Fetch user and set WordPress context
5955
const user = await fetchWordPressUser();
6056
toolbar.setWordPressContext({
6157
user: {
6258
id: user.id,
6359
name: user.name,
64-
email: user.email
60+
email: user.email,
6561
},
6662
site: {
67-
url: 'http://localhost:8888',
68-
adminUrl: 'http://localhost:8888/wp-admin'
69-
}
63+
url: "http://localhost:8888",
64+
adminUrl: "http://localhost:8888/wp-admin",
65+
},
7066
});
7167
```
7268

@@ -77,27 +73,27 @@ const { state, nodes } = useToolbar(toolbar);
7773

7874
// Subscribe to state changes
7975
useEffect(() => {
80-
console.log('Toolbar state:', state);
76+
console.log("Toolbar state:", state);
8177
}, [state]);
8278
```
8379

8480
### 4. Custom Node Registration
8581

8682
```tsx
87-
toolbar.register('home', 'Home', () => {
88-
router.push('/');
83+
toolbar.register("home", "Home", () => {
84+
router.push("/");
8985
});
9086
```
9187

9288
## Features
9389

94-
- React hooks (`useToolbar`, `useToolbarState`, `useToolbarNodes`)
95-
- Next.js App Router (App Directory)
96-
- TypeScript support
97-
- Framework-agnostic state management
98-
- WordPress context integration
99-
- Real WordPress data integration
100-
- WPGraphQL support
90+
- React hooks (`useToolbar`, `useToolbarState`, `useToolbarNodes`)
91+
- Next.js App Router (App Directory)
92+
- TypeScript support
93+
- Framework-agnostic state management
94+
- WordPress context integration
95+
- Real WordPress data integration
96+
- WPGraphQL support
10197

10298
## Project Structure
10399

@@ -153,20 +149,18 @@ npm run wp:destroy
153149
## WordPress Setup
154150

155151
The wp-env configuration includes:
152+
156153
- WordPress with WPGraphQL plugin
157154
- Admin credentials: `admin` / `password`
158155
- GraphQL endpoint: `http://localhost:8888/?graphql`
159156
- REST API endpoint: `http://localhost:8888/?rest_route=/wp/v2/...`
160157
- Pretty permalinks enabled
161158
- CORS headers enabled for localhost:3000
162159

163-
## Environment Configuration
164-
165-
The example uses standard ports (3000 for frontend, 8888 for WordPress) to match other hwptoolkit examples.
166-
167160
## TypeScript Support
168161

169162
The example includes full TypeScript support with proper types for:
163+
170164
- Toolbar state and nodes
171165
- WordPress API responses
172166
- React hook return types

packages/toolbar/examples/vanilla/.gitignore

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/toolbar/examples/vanilla/.wp-env.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"WP_HOME": "http://localhost:8888",
1717
"HEADLESS_FRONTEND_URL": "http://localhost:3000"
1818
},
19+
"mappings": {
20+
".htaccess": "./wp-env/setup/.htaccess"
21+
},
1922
"lifecycleScripts": {
2023
"afterStart": "wp-env run cli -- wp theme activate twentytwentyfour && wp-env run cli -- wp rewrite structure '/%postname%/' && wp-env run cli -- wp rewrite flush"
2124
}

packages/toolbar/examples/vanilla/README.md

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ In this example we show how to integrate the Headless WordPress Toolbar into a v
77
> [!IMPORTANT]
88
> Docker Desktop needs to be installed to run WordPress locally.
99
10+
1. Create a `.env.local` file in the `example-app` directory with the following content:
1011

11-
1. Create a `.env.local` file in the `examples/next/toolbar-demo` directory with the following content:
12-
```env
12+
```env
1313
VITE_FRONTEND_PORT=3000
1414
VITE_WP_URL=http://localhost:8888
1515
VITE_WP_PORT=8888
1616
VITE_WP_TEST_PORT=8889
17-
```
17+
```
1818

1919
2. Run `npm run example:setup` to install dependencies and configure the local WP server.
2020
3. Run `npm run example:start` `to start the WP server and Vite development server.
2121

2222
The example will be available at:
23+
2324
- **Frontend**: http://localhost:3000
2425
- **WordPress**: http://localhost:8888
2526
- **WordPress Admin**: http://localhost:8888/wp-admin (`admin` / `password`)
@@ -33,16 +34,16 @@ The example will be available at:
3334
### 1. Basic Toolbar Integration
3435

3536
```javascript
36-
import { Toolbar, VanillaRenderer } from '@wpengine/hwp-toolbar';
37-
import '@wpengine/hwp-toolbar/styles';
37+
import { Toolbar, VanillaRenderer } from "@wpengine/hwp-toolbar";
38+
import "@wpengine/hwp-toolbar/styles";
3839

3940
const toolbar = new Toolbar({
4041
onPreviewChange: (enabled) => {
41-
console.log('Preview mode:', enabled);
42-
}
42+
console.log("Preview mode:", enabled);
43+
},
4344
});
4445

45-
const renderer = new VanillaRenderer(toolbar, 'toolbar');
46+
const renderer = new VanillaRenderer(toolbar, "toolbar");
4647
```
4748

4849
### 2. WordPress Integration
@@ -51,28 +52,28 @@ The example fetches real data from WordPress:
5152

5253
```javascript
5354
// Fetch WordPress user via REST API
54-
const response = await fetch('http://localhost:8000/?rest_route=/wp/v2/users/1');
55+
const response = await fetch("http://localhost:8000/?rest_route=/wp/v2/users/1");
5556
const user = await response.json();
5657

5758
toolbar.setWordPressContext({
5859
user: {
5960
id: user.id,
6061
name: user.name,
61-
email: user.email
62+
email: user.email,
6263
},
6364
site: {
64-
url: 'http://localhost:8000',
65-
adminUrl: 'http://localhost:8000/wp-admin'
66-
}
65+
url: "http://localhost:8000",
66+
adminUrl: "http://localhost:8000/wp-admin",
67+
},
6768
});
6869
```
6970

7071
### 3. GraphQL Posts
7172

7273
```javascript
73-
const response = await fetch('http://localhost:8000/?graphql', {
74-
method: 'POST',
75-
headers: { 'Content-Type': 'application/json' },
74+
const response = await fetch("http://localhost:8000/?graphql", {
75+
method: "POST",
76+
headers: { "Content-Type": "application/json" },
7677
body: JSON.stringify({
7778
query: `
7879
query GetPosts {
@@ -85,38 +86,37 @@ const response = await fetch('http://localhost:8000/?graphql', {
8586
}
8687
}
8788
}
88-
`
89-
})
89+
`,
90+
}),
9091
});
9192
```
9293

9394
### 4. Custom Node Registration
9495

9596
```javascript
96-
toolbar.register('home', 'Home', () => {
97-
window.location.href = '/';
97+
toolbar.register("home", "Home", () => {
98+
window.location.href = "/";
9899
});
99100
```
100101

101102
### 5. State Management
102103

103104
```javascript
104105
toolbar.subscribe((nodes, state) => {
105-
console.log('Toolbar state updated:', state);
106+
console.log("Toolbar state updated:", state);
106107
});
107108
```
108109

109110
## Features
110111

111-
- ✅ Vanilla JavaScript (no framework)
112-
- ✅ Vite for fast development
113-
- ✅ TypeScript support (types available)
114-
- ✅ WordPress toolbar integration
115-
- ✅ State management example
116-
- ✅ Custom node registration
117-
- ✅ Dark/light mode support
118-
- ✅ Real WordPress data integration
119-
- ✅ WPGraphQL support
112+
- Vanilla JavaScript (no framework)
113+
- Vite for fast development
114+
- WordPress toolbar integration
115+
- State management example
116+
- Custom node registration
117+
- Dark/light mode support
118+
- Real WordPress data integration
119+
- WPGraphQL support
120120

121121
## Project Structure
122122

@@ -159,32 +159,20 @@ npm run wp:destroy
159159
## WordPress Setup
160160

161161
The wp-env configuration includes:
162+
162163
- WordPress with WPGraphQL plugin
163164
- Admin credentials: `admin` / `password`
164165
- GraphQL endpoint: `http://localhost:8000/?graphql`
165166
- REST API endpoint: `http://localhost:8000/?rest_route=/wp/v2/...`
166167
- Pretty permalinks enabled
167168
- CORS headers enabled for localhost:3000
168169

169-
## Environment Configuration
170-
171-
The example uses standard ports (3000 for frontend, 8000 for WordPress) to match other hwptoolkit examples.
172-
173-
To customize ports, create a `.env` file in the `example-app/` directory:
174-
175-
```
176-
VITE_FRONTEND_PORT=3000
177-
VITE_WP_URL=http://localhost:8888
178-
VITE_WP_PORT=8888
179-
VITE_WP_TEST_PORT=8889
180-
```
181-
182170
## Styling
183171

184172
The example imports the base toolbar styles and adds custom demo styling:
185173

186174
```javascript
187-
import '@wpengine/hwp-toolbar/styles';
175+
import "@wpengine/hwp-toolbar/styles";
188176
```
189177

190178
Custom styles can override CSS variables:

0 commit comments

Comments
 (0)