Send a Webhook When a Post Is Published
Posts a small JSON payload (ID, title, link, author and date) to a URL of your choice whenever a post goes live, to trigger a static site build, a Zapier flow or a chat notification.
Opens WPIDE on
Code
// Where to send the webhook (leave empty to disable).
$wpide_webhook_url = 'https://example.com/hooks/wordpress';
// Post types that trigger it.
$wpide_webhook_post_types = array( 'post' );
add_action( 'transition_post_status', function ( $new_status, $old_status, $post ) use ( $wpide_webhook_url, $wpide_webhook_post_types ) {
if ( '' === $wpide_webhook_url || 'publish' !== $new_status || 'publish' === $old_status || ! in_array( $post->post_type, $wpide_webhook_post_types, true ) ) {
return;
}
if ( wp_is_post_revision( $post ) || wp_is_post_autosave( $post ) ) {
return;
}
wp_remote_post( $wpide_webhook_url, array(
'timeout' => 3,
'blocking' => false,
'headers' => array( 'Content-Type' => 'application/json' ),
'body' => wp_json_encode( array(
'event' => 'post_published',
'id' => $post->ID,
'title' => get_the_title( $post ),
'link' => get_permalink( $post ),
'author' => get_the_author_meta( 'display_name', $post->post_author ),
'date' => get_post_time( 'c', true, $post ),
'site' => home_url( '/' ),
) ),
) );
}, 10, 3 );
Notes
Set $wpide_webhook_url; the request is non-blocking with a short timeout so publishing never waits for the receiving service.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.