Skip to content

Commit 8bf56fd

Browse files
authored
Merge pull request #267 from wpengine/feature-webhooks-plugin-documentation
feat: add webhooks plugin documentation
2 parents 53b1482 + 155def0 commit 8bf56fd

File tree

22 files changed

+781
-1415
lines changed

22 files changed

+781
-1415
lines changed

examples/next/webhooks-isr/README.md

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ This integration enables seamless communication between a WordPress backend and
44

55
## Features
66

7-
*Incremental Static Regeneration (ISR) Showcase – Demonstrates Next.js ISR fully working with WordPress-triggered webhooks.
7+
* Incremental Static Regeneration (ISR) Showcase – Demonstrates Next.js ISR fully working with WordPress-triggered webhooks.
88

9-
*On-Demand Revalidation – Webhooks notify Next.js to revalidate specific pages when WordPress content changes.
9+
* On-Demand Revalidation – Webhooks notify Next.js to revalidate specific pages when WordPress content changes.
1010

11-
*Relative Path Payloads – Webhook payloads send clean relative paths (e.g., /posts/my-post) for accurate revalidation.
11+
* Relative Path Payloads – Webhook payloads send clean relative paths (e.g., /posts/my-post) for accurate revalidation.
1212

13-
*Secure Webhook Requests – Uses secret tokens in headers to authenticate webhook calls.
13+
* Secure Webhook Requests – Uses secret tokens in headers to authenticate webhook calls.
1414

15-
*Flexible HTTP Methods & Headers – Supports POST requests with custom headers for integration flexibility.
15+
* Flexible HTTP Methods & Headers – Supports POST requests with custom headers for integration flexibility.
1616

17-
*WordPress Native Integration – Uses WordPress Custom Post Types and hooks for managing webhooks.
17+
* WordPress Native Integration – Uses WordPress Custom Post Types and hooks for managing webhooks.
1818

19-
*Extensible & Developer Friendly – Easily customizable payloads and event triggers via WordPress filters and actions.
19+
* Extensible & Developer Friendly – Easily customizable payloads and event triggers via WordPress filters and actions.
2020

2121
## Prerequisites
2222

@@ -33,36 +33,31 @@ NEXT_PUBLIC_WORDPRESS_URL=http://your-wordpress-site.com
3333
WEBHOOK_REVALIDATE_SECRET=your_webhook_secret_token
3434
```
3535

36-
### Creating a Test Webhook in WordPress
37-
Add this PHP snippet to your theme’s `functions.php` or a custom plugin to create a webhook that triggers on post updates and calls your Next.js revalidation API:
36+
### Creating a Webhook via the Admin UI
37+
You can easily create and manage webhooks directly from the WordPress admin dashboard, without writing any code. The intuitive Webhooks UI allows you to specify the event, target URL, HTTP method, and custom headers for each webhook. This makes it simple to connect WordPress events to external services and automate your workflows.
3838

39-
```php
40-
function create_test_post_published_webhook() {
41-
// Get the repository instance from your plugin
42-
$repository = \WPGraphQL\Webhooks\Plugin::instance()->get_repository();
43-
44-
// Define webhook properties
45-
$name = 'Test Post Published Webhook';
46-
$event = 'post_updated';
47-
$url = 'http://localhost:3000/api/revalidate'; // Update to your Next.js API URL
48-
$method = 'POST';
49-
50-
$headers = [
51-
'X-Webhook-Secret' => 'your_webhook_secret_token', // Must match Next.js secret
52-
'Content-Type' => 'application/json',
53-
];
54-
$result = $repository->create( $name, $event, $url, $method, $headers );
55-
56-
if ( is_wp_error( $result ) ) {
57-
error_log( 'Failed to create webhook: ' . $result->get_error_message() );
58-
} else {
59-
error_log( 'Webhook created successfully with ID: ' . $result );
60-
}
61-
}
39+
Follow these steps to create a webhook using the UI:
6240

63-
// Run once, for example on admin_init or manually trigger it
64-
add_action( 'admin_init', 'create_test_post_published_webhook' );
65-
```
41+
1. **Navigate to the Webhooks Admin Page**
42+
In your WordPress dashboard, go to the sidebar and click on Webhooks.
43+
44+
2. **Click "Add New" or Edit an Existing Webhook**
45+
To create a new webhook, click the Add New button.
46+
To edit, click the webhook you want to update.
47+
48+
3. **Fill in the Webhook Details**
49+
* **Name**: Enter a descriptive name (e.g., Test Post Published Webhook).
50+
* **Event**: Select the event that will trigger the webhook (e.g., Post Updated).
51+
* **URL**: Enter the endpoint URL where the webhook payload should be sent (e.g., http://localhost:3000/api/revalidate).
52+
**HTTP Method**: Choose the HTTP method (e.g., POST).
53+
**Headers**: Add any HTTP headers required. For example:
54+
**X-Webhook-Secret**: d9f8a7e2b6c4d3f1a9e7b5c2df1f0e8a3
55+
**Content-Type**: application/json
56+
* Click Add Header to add more headers as needed.
57+
4. **Save the Webhook**
58+
Click Create Webhook (or Update Webhook if editing) to save your settings.
59+
60+
![Create Webhook view](./screenshots/create_webhook-ui.png)
6661

6762
## Modifying the Webhook Payload to Send Relative Paths
6863
Add this filter to your WordPress plugin or theme to ensure the webhook payload sends a relative path (required by Next.js revalidate API):
@@ -95,7 +90,44 @@ add_filter( 'graphql_webhooks_payload', function( array $payload, $webhook ) {
9590
}, 10, 2 );
9691
```
9792

93+
## Testing the Integration with the Example Project
94+
95+
To verify that your webhook integration is working correctly, follow these steps:
96+
97+
1. Run the example project in production mode. (see ## Command Reference section)
98+
99+
2. In WordPress, update or create a new post, for example with the slug `/posts/new-post`.
98100

101+
3. Visit the corresponding page on your headless site at:
102+
103+
`http://localhost:3000/posts/new-post`
104+
105+
106+
Refresh the page to see the updated content served via Incremental Static Regeneration (ISR).
107+
108+
4. Check the Next.js server logs. You should see logs indicating that the webhook revalidation request was received and processed successfully, similar to the following:
109+
110+
```bash
111+
[Webhook] Received revalidation request
112+
[Webhook] Secret from header: Provided
113+
[Webhook] Expected secret is set: Yes
114+
[Webhook] Secret token validated successfully
115+
[Webhook] Request body parsed: {
116+
key: 'cG9zdDoyNDI=',
117+
path: '/posts/new-post/',
118+
graphql_endpoint: 'mysite.local/graphql',
119+
smart_cache_keys: [ 'cG9zdDoyNDI=' ],
120+
_webhook_meta: {
121+
sent_at: '2025-06-24 12:19:15',
122+
webhook_id: 254,
123+
webhook_name: 'Test Post Published Webhook',
124+
event_type: 'post_updated'
125+
}
126+
}
127+
[Webhook] Path to revalidate: /posts/new-post/
128+
```
129+
130+
This confirms that the webhook triggered by your WordPress post update is successfully revalidating the page on your headless Next.js site.
99131

100132
## How It Works
101133
This integration:
@@ -143,7 +175,6 @@ Congratulations, WordPress should now be fully set up.
143175

144176
> **Note:** The login details for the admin is username "admin" and password "password"
145177
146-
147178
## Command Reference
148179

149180
| Command | Description |
239 KB
Loading

plugins/wp-graphql-headless-webhooks/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ A WordPress plugin that extends [WPGraphQL](https://www.wpgraphql.com/) to suppo
2323

2424
Clone the repository or download the latest release and place it in your WordPress `plugins` directory:
2525

26+
## Documentation
27+
28+
For detailed usage instructions, developer references, and examples, please visit the [Documentation](docs/index.md) folder included with this plugin.
29+
30+
2631
## License
2732
WP GPL 2

plugins/wp-graphql-headless-webhooks/assets/js/admin.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
(function ($) {
66
'use strict';
7-
87
$( document ).ready(
98
function () {
109
// Handle adding new header fields

0 commit comments

Comments
 (0)