Skip to content

Commit 776536f

Browse files
authored
Merge pull request #521 from wpengine/fix-hwp-previews-examples
docs(previews): Fix examples, update example readmes
2 parents 38e3e7c + 3838dcc commit 776536f

File tree

4 files changed

+49
-46
lines changed

4 files changed

+49
-46
lines changed

plugins/hwp-previews/examples/hwp-preview-rest/.wp-env.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"phpVersion": "7.4",
33
"plugins": [
44
"https://github.com/Tmeister/wp-api-jwt-auth/archive/refs/tags/1.3.8.zip",
5-
"../../../plugins/hwp-previews"
5+
"https://github.com/wpengine/hwptoolkit/releases/download/%40wpengine%2Fhwp-previews-wordpress-plugin-0.0.12/hwp-previews.zip"
66
],
77
"config": {
88
"WP_DEBUG": true,

plugins/hwp-previews/examples/hwp-preview-rest/README.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
# Example: Headless WordPress Previews with Nextjs App Router and REST API
1+
---
2+
title: "Headless WordPress Previews with Next.js App Router and REST API"
3+
description: "This demonstrates the usage of HWP Previews with Next.js App Router and REST API. It uses JWT authentication to fetch posts in draft status and implements a login modal for front-end authentication, showcasing an alternative approach to the Draft Mode implementation."
4+
---
5+
6+
# Example: Headless WordPress Previews with Next.js App Router and REST API
27

38
> [!NOTE]
4-
> Check out [HWP Previews WPGraphQL example](../hwp-preview-wpgraphql) if you need the previews implementation with Nextjs pages router, Draft Mode or WordPress Application Passwords.
9+
> Check out [HWP Previews WPGraphQL example](../hwp-preview-wpgraphql) if you need the previews implementation with Next.js pages router, Draft Mode or WordPress Application Passwords.
510
611
## Overview
712

8-
The purpose of this example is to showcase different use cases of HWP Previews. The example demonstrates the usage of [HWP Previews](https://github.com/wpengine/hwptoolkit/tree/main/plugins/hwp-previews) with Nextjs App Router and REST API. Example uses credentials authentication to fetch the posts in draft status. Unlike [HWP Previews WPGraphQL example](../hwp-preview-wpgraphql) this example don't use [Draft Mode](https://nextjs.org/docs/pages/guides/draft-mode).
13+
The purpose of this example is to showcase different use cases of HWP Previews. The example demonstrates the usage of [HWP Previews](https://github.com/wpengine/hwptoolkit/tree/main/plugins/hwp-previews) with Next.js App Router and REST API. Example uses credentials authentication to fetch the posts in draft status. Unlike [HWP Previews WPGraphQL example](../hwp-preview-wpgraphql) this example doesn't use [Draft Mode](https://nextjs.org/docs/pages/guides/draft-mode).
914

1015
The example includes a wp-env setup, which will allow you to build and start this example quickly. With this wp-env setup, you don't need to have a separate WordPress instance or demo data to inspect the example.
1116

12-
> [!CAUTION]
13-
> The HWP Previews plugin is currently in beta. While it's more stable than earlier versions, you may still encounter occasional bugs or incomplete features. Regular updates will continue as development progresses. Use with care and consider providing feedback to help improve the plugin.
17+
## What does this example do
18+
19+
1. Preview posts, pages and custom post types using WordPress REST API
20+
2. Use JWT authentication for secure access to draft content
21+
3. Next.js App Router implementation without Draft Mode
22+
4. Login modal for front-end authentication
23+
5. Configured WordPress instance with demo data and required plugins, using wp-env.
1424

1525
## Screenshots
1626

@@ -52,47 +62,42 @@ The example includes a wp-env setup, which will allow you to build and start thi
5262

5363
**Note** Please make sure you have all prerequisites installed as mentioned above and Docker running (`docker ps`)
5464

55-
### Setup Repository and Packages
65+
### 1. Setup Repository and Packages
5666

5767
- Clone the repo `git clone https://github.com/wpengine/hwptoolkit.git`
5868
- Install packages `cd hwptoolkit && npm install`
5969

60-
### Build and start the application
70+
### 2. Build and start the application
6171

62-
- `cd examples/next/hwp-preview-rest`
72+
- `cd plugins/hwp-previews/examples/hwp-preview-rest`
6373
- Then run `npm run example:build` will build and start your application.
64-
- This does the following:
65-
- Unzips `wp-env/uploads.zip` to `wp-env/uploads` which is mapped to the wp-content/uploads directory for the Docker container.
66-
- Starts up [wp-env](https://developer.wordpress.org/block-editor/getting-started/devenv/get-started-with-wp-env/)
67-
- Imports the database from [wp-env/db/database.sql](wp-env/db/database.sql)
68-
- Install Next.js dependencies for `example-app`
69-
- Runs the Next.js dev script
7074

71-
Congratulations, WordPress should now be fully set up.
75+
This starts the wp-env instance and frontend application. You can access them now, but one more installation step remains.
76+
77+
> [!IMPORTANT]
78+
> After logging in to the WordPress instance, ensure that all installed plugins are activated for this example to work properly.
7279
7380
| Frontend | Admin |
7481
| ---------------------- | ------------------------------- |
7582
| http://localhost:3000/ | http://localhost:8888/wp-admin/ |
7683

7784
> **Note:** The login details for the admin is username "admin" and password "password"
7885
79-
### Add environment variable to the Nextjs
80-
81-
Create a .env file under `examples/next/hwp-preview-rest/example-app` and add copy-paste the environment variable below:
86+
### 3. Add environment variable to the Next.js application
8287

88+
Create a .env file under `plugins/hwp-previews/examples/hwp-preview-rest/example-app` and add the environment variable below:
8389

8490
```bash
8591
NEXT_PUBLIC_WORDPRESS_URL=http://localhost:8888
8692
```
8793

88-
### Usage
94+
> [!CAUTION]
95+
> This setup is intended for demonstration purposes only. For production use, you should consider the security implications and implement appropriate measures based on your project's specific needs.
8996
90-
After completing this step, login to your [WordPress instance](http://localhost:8888) and preview the posts as usual. Since all of the settings are configured for this example, clicking the preview button should redirect you to the front-end URL. To see the draft posts and pages you will need to login your WordPress account on front-end website, using the same credentials.
9197

92-
If you want to learn more about the preview plugin, check out [the documentation](/plugins/hwp-previews/README.md).
98+
After completing this step, clicking the preview button in wp-admin should open the preview in your front-end app. Login with your admin credentials on the frontend to enable draft preview functionality.
9399

94-
> [!CAUTION]
95-
> This setup is intended for demonstration purposes only. For production use, you should consider the security implications and implement appropriate measures based on your project’s specific needs.
100+
If you want to learn more about the preview plugin, check out [the documentation](../../../../docs/plugins/hwp-previews/index.md).
96101

97102
### Command Reference
98103

plugins/hwp-previews/examples/hwp-preview-wpgraphql/.wp-env.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"https://github.com/wp-graphql/wp-graphql/releases/latest/download/wp-graphql.zip",
55
"https://github.com/AdvancedCustomFields/acf/releases/latest/download/advanced-custom-fields.zip",
66
"https://github.com/wp-graphql/wpgraphql-acf/releases/latest/download/wpgraphql-acf.zip",
7-
"../../"
7+
"https://github.com/wpengine/hwptoolkit/releases/download/%40wpengine%2Fhwp-previews-wordpress-plugin-0.0.12/hwp-previews.zip"
88
],
99
"config": {
1010
"WP_DEBUG": true,

plugins/hwp-previews/examples/hwp-preview-wpgraphql/README.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
---
2+
title: "Headless WordPress Previews with Next.js Pages Router and WPGraphQL"
3+
description: "This demonstrates the usage of HWP Previews with Next.js Pages Router and WPGraphQL. It uses WordPress Application Passwords for authentication and implements Draft Mode for secure preview functionality."
4+
---
5+
16
# Example: Headless WordPress Previews with Nextjs Draft Mode
27

38
## Overview
49

5-
This example shows the HWP Previews plugin in action. Example implements the [Draft Mode](https://nextjs.org/docs/pages/guides/draft-mode) of Nextjs. It uses WordPress [Application Passwords](https://wordpress.com/support/security/two-step-authentication/application-specific-passwords/) for the authentication and WPGraphQL for data fetching.
10+
This example shows the HWP Previews plugin in action. Example implements the [Draft Mode](https://nextjs.org/docs/pages/guides/draft-mode) of Next.js. It uses WordPress [Application Passwords](https://wordpress.com/support/security/two-step-authentication/application-specific-passwords/) for the authentication and WPGraphQL for data fetching.
611

712
The example includes a wp-env setup, which will allow you to build and start this example quickly. With this wp-env setup, you don't need to have a separate WordPress instance or demo data to inspect the example.
813

9-
> [!CAUTION]
10-
> The HWP Previews plugin is currently in an beta state. It's still under active development, so you may encounter bugs or incomplete features. Updates will be rolled out regularly. Use with caution and provide feedback if possible.
11-
1214
## What does this example do
1315

14-
1. `/api/preview/` route to enable Nextjs Draft Mode
15-
2. `/api/disable-preview` route to disable Nextjs Draft Mode
16+
1. `/api/preview/` route to enable Next.js Draft Mode
17+
2. `/api/disable-preview` route to disable Next.js Draft Mode
1618
3. Preview posts, pages and custom post types
1719
4. Use WordPress Application Password as an authentication method
18-
5. Configured WordPress instance with demo data and required plugins, using wp-env
20+
5. Configured WordPress instance with demo data and required plugins, using wp-env.
1921

2022
## Screenshots
2123

@@ -55,34 +57,30 @@ The example includes a wp-env setup, which will allow you to build and start thi
5557

5658
**Note** Please make sure you have all prerequisites installed as mentioned above and Docker running (`docker ps`)
5759

58-
### Setup Repository and Packages
60+
### 1. Setup Repository and Packages
5961

6062
- Clone the repo `git clone https://github.com/wpengine/hwptoolkit.git`
6163
- Install packages `cd hwptoolkit && npm install`
6264

63-
### Build and start the application
65+
### 2. Build and start the application
6466

6567
- `cd plugins/hwp-previews/examples/hwp-preview-wpgraphql`
6668
- Then run `npm run example:build` will build and start your application.
67-
- Copy .env.local to .env under example-app `cp example-app/.env.local example-app/.env`
68-
- This does the following:
69-
- Unzips `wp-env/uploads.zip` to `wp-env/uploads` which is mapped to the wp-content/uploads directory for the Docker container.
70-
- Starts up [wp-env](https://developer.wordpress.org/block-editor/getting-started/devenv/get-started-with-wp-env/)
71-
- Imports the database from [wp-env/db/database.sql](wp-env/db/database.sql)
72-
- Install Next.js dependencies for `example-app`
73-
- Runs the Next.js dev script
7469

75-
Congratulations, WordPress should now be fully set up.
70+
This starts the wp-env instance and frontend application. You can access them now, but one more installation step remains.
71+
72+
> [!IMPORTANT]
73+
> After logging in to the WordPress instance, ensure that all installed plugins are activated for this example to work properly.
7674
7775
| Frontend | Admin |
7876
| ---------------------- | ------------------------------- |
7977
| http://localhost:3000/ | http://localhost:8888/wp-admin/ |
8078

8179
> **Note:** The login details for the admin is username "admin" and password "password"
8280
83-
### Add environment variables to the Nextjs
81+
### 3. Add environment variables to the Nextjs
8482

85-
In this step we will create an Application Password and a secret preview token for the previews. At the end we'll prepare a .env file for the Nextjs application and put it inside `examples/next/hwp-preview-wpgraphql/example-app` folder. Below are the step by step guide to achieve this.
83+
In this step we will create an Application Password and a secret preview token for the previews. At the end we'll prepare a .env file for the Next.js application and put it inside `plugins/hwp-previews/examples/hwp-preview-wpgraphql/example-app` folder. Below are the step by step guide to achieve this.
8684

8785
1. Login to your newly created [wp-env instance](http://localhost:8888/wp-admin/). You can find the credentials above
8886
2. a. Follow [these instructions](https://wordpress.com/support/security/two-step-authentication/application-specific-passwords/) to create an Application Password. At the end it should look like this:
@@ -95,7 +93,7 @@ In this step we will create an Application Password and a secret preview token f
9593

9694
![index](./screenshots/preview_token.png "Change secret preview token")
9795

98-
4. Create a .env file under `examples/next/hwp-preview-wpgraphql/example-app` and add these values inside:
96+
4. Create a .env file under `plugins/hwp-previews/examples/hwp-preview-wpgraphql/example-app` and add these values inside:
9997

10098
```bash
10199
NEXT_PUBLIC_WORDPRESS_URL=http://localhost:8888
@@ -110,7 +108,7 @@ WP_PREVIEW_SECRET=YOUR-SECRET-PREVIEW-TOKEN
110108
111109
After completing this step, clicking the preview button in wp-admin should open the preview in your front-end app.
112110

113-
If you want to learn more about the preview plugin, check out [the documentation](/plugins/hwp-previews/README.md).
111+
If you want to learn more about the preview plugin, check out [the documentation](../../../../docs/plugins/hwp-previews/index.md).
114112

115113
### Command Reference
116114

0 commit comments

Comments
 (0)