WordPress to Social Media: Auto-Post New Posts via Webhook
Fire an outbound webhook when a WordPress post publishes and turn it into a social post with PostGate’s API — no official WordPress plugin needed.
Hook WordPress’s built-in publish_post action and call PostGate’s POST /api/posts with wp_remote_post() from a tiny plugin: build a caption from the post excerpt and permalink, attach the featured image if set, and send an Idempotency-Key tied to the post ID. Define the API key in wp-config.php and guard against duplicate firings with post meta.
There's no official PostGate plugin in the WordPress plugin directory. What WordPress does have, built into core, is publish_post — an action hook that fires when a post transitions to published — and wp_remote_post(), its built-in HTTP client. Together they're enough to call PostGate's API directly from a tiny plugin, with no third-party service in between.
A small plugin that calls PostGate on publish
Save this as its own plugin file (or a must-use plugin under wp-content/mu-plugins/) and activate it. It builds a caption from the post title and permalink, attaches the featured image if one is set, and posts it through PostGate with an Idempotency-Key tied to the WordPress post ID.
<?php
/**
* Plugin Name: PostGate Auto-Publish
* Description: Publishes a social post via PostGate when a post is published.
*/
add_action('publish_post', function ($post_id, $post) {
if (wp_is_post_revision($post_id)) {
return;
}
// Guard against publish_post firing again on a later save of an
// already-published post — see the note below.
if (get_post_meta($post_id, '_postgate_published', true)) {
return;
}
$excerpt = wp_trim_words(wp_strip_all_tags($post->post_content), 40);
$body = $excerpt . "\n\n" . get_permalink($post_id);
$thumbnail = get_the_post_thumbnail_url($post_id, 'large');
$payload = [
'post' => ['body' => $body],
'profiles' => ['pro_x123', 'pro_linkedin456'],
];
if ($thumbnail) {
$payload['media'] = [$thumbnail];
}
$response = wp_remote_post('https://postgate.studio/api/posts?profile_group_id=grp_acme123', [
'headers' => [
'Authorization' => 'Bearer ' . POSTGATE_API_KEY,
'Content-Type' => 'application/json',
'Idempotency-Key' => 'wp-post-' . $post_id,
],
'body' => wp_json_encode($payload),
'timeout' => 15,
]);
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) < 300) {
update_post_meta($post_id, '_postgate_published', true);
}
}, 10, 2);Keep the API key out of the plugin file
Define POSTGATE_API_KEY as a constant in wp-config.php, which sits outside version control on most WordPress hosts, rather than hard-coding it in the plugin:
define('POSTGATE_API_KEY', 'pg_live_xxxxxxxxxxxxxxxx');Why does the meta-flag guard matter?
publish_post is tied to a post's status transitioning to publish, and depending on your editor and any other plugins touching the post, a later edit-and-save on an already-published post can trigger the same hook again. The _postgate_published post-meta check above stops that from firing a second social post for one article. The PostGate Idempotency-Key is a second, independent safety net in case the meta check itself races with a concurrent save.
Prefer not to touch PHP? Use a webhook plugin
Several WordPress plugins (for example WP Webhooks) let you fire an outbound webhook from the publish_post trigger without writing code. Whether you can set a custom Authorization header from the plugin's UI depends on the specific plugin and its tier — check its documentation before relying on it — but where that's supported, point the webhook straight at https://postgate.studio/api/posts with the same headers and JSON body shown above. Where custom headers aren't available, send the webhook to n8n, Make, or Zapier instead and let its HTTP module attach the PostGate auth header — see calling PostGate from n8n, Make, and Zapier.
Frequently asked questions
Is there an official PostGate plugin for WordPress?
No — there’s no official PostGate plugin in the WordPress plugin directory. What WordPress does have, built into core, is the publish_post action hook and wp_remote_post(), its built-in HTTP client. Together they’re enough to call PostGate’s API directly from a tiny plugin, with no third-party service in between.
Why does my WordPress publish hook fire twice for one post?
publish_post is tied to a post’s status transitioning to publish, and depending on your editor and any other plugins touching the post, a later edit-and-save on an already-published post can trigger the same hook again. A _postgate_published post-meta check stops that from firing a second social post, with the Idempotency-Key as a second, independent safety net if the meta check races with a concurrent save.
Where should the PostGate API key live in WordPress?
Define it as a constant in wp-config.php, which sits outside version control on most WordPress hosts, rather than hard-coding it in the plugin file.
Can I connect WordPress to social media without writing PHP?
Yes. Several WordPress plugins (for example WP Webhooks) let you fire an outbound webhook from the publish_post trigger without code. Where the plugin supports a custom Authorization header, point the webhook straight at PostGate’s /api/posts; where it doesn’t, send the webhook to n8n, Make, or Zapier and let its HTTP module attach the auth header.
