Hey there, fellow JavaScript dev! Ready to dive into the world of WordPress.com webhooks? Buckle up, because we're about to turbocharge your WordPress.com integrations with some real-time goodness.
Webhooks are like the cool kids of the API world. Instead of constantly pestering a server for updates, webhooks let the server come to you when something interesting happens. It's like having a personal assistant for your app, keeping you in the loop without all the hassle.
Make sure you've got:
First things first, let's get you set up with the WordPress.com API:
We're using OAuth 2.0 here, because security is sexy. Here's a quick snippet to get your access token:
const axios = require('axios'); const qs = require('querystring'); const getAccessToken = async (clientId, clientSecret, code) => { const response = await axios.post('https://public-api.wordpress.com/oauth2/token', qs.stringify({ client_id: clientId, client_secret: clientSecret, code: code, grant_type: 'authorization_code', redirect_uri: 'YOUR_REDIRECT_URI' })); return response.data.access_token; };
Now for the fun part – setting up your webhook subscription. WordPress.com offers a bunch of topics you can subscribe to, like post updates, comments, and more.
Here's how you create a subscription:
const createWebhookSubscription = async (accessToken, topic, targetUrl) => { const response = await axios.post('https://public-api.wordpress.com/rest/v1.1/sites/YOUR_SITE_ID/webhooks/new', { topic: topic, delivery_url: targetUrl }, { headers: { Authorization: `Bearer ${accessToken}` } }); return response.data; };
Time to set up a simple Express server to catch those webhooks:
const express = require('express'); const crypto = require('crypto'); const app = express(); app.use(express.json()); app.post('/webhook', (req, res) => { const signature = req.get('X-WordPress-Signature'); const payload = JSON.stringify(req.body); // Verify the signature (replace 'YOUR_SECRET' with your actual secret) const expectedSignature = crypto.createHmac('sha256', 'YOUR_SECRET').update(payload).digest('hex'); if (signature === expectedSignature) { console.log('Webhook received:', req.body); res.sendStatus(200); } else { console.error('Invalid signature'); res.sendStatus(403); } }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
Want to see what subscriptions you've got? Update them? Give them the boot? We've got you covered:
// List subscriptions const listSubscriptions = async (accessToken) => { const response = await axios.get('https://public-api.wordpress.com/rest/v1.1/sites/YOUR_SITE_ID/webhooks', { headers: { Authorization: `Bearer ${accessToken}` } }); return response.data; }; // Update a subscription const updateSubscription = async (accessToken, webhookId, newUrl) => { const response = await axios.post(`https://public-api.wordpress.com/rest/v1.1/sites/YOUR_SITE_ID/webhooks/${webhookId}`, { delivery_url: newUrl }, { headers: { Authorization: `Bearer ${accessToken}` } }); return response.data; }; // Delete a subscription const deleteSubscription = async (accessToken, webhookId) => { await axios.post(`https://public-api.wordpress.com/rest/v1.1/sites/YOUR_SITE_ID/webhooks/${webhookId}/delete`, {}, { headers: { Authorization: `Bearer ${accessToken}` } }); };
Running into issues? Don't sweat it:
And there you have it! You're now armed and dangerous with WordPress.com webhook knowledge. Remember, with great power comes great responsibility – use these webhooks wisely, and your WordPress.com integrations will be the talk of the town.
Now go forth and webhook all the things! 🚀