# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: webhook, publishing, integrations
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/rest-webhook-on-publish/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Send a Webhook When a Post Is Published" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
