WordPress Publish Post Hook: Send Events

When a WordPress post is published or an existing published post is edited, you may want another service to hear about the change. A WordPress action hook runs a callback at a defined point in a request, and that callback can send an event to a receiver.

I’ll show you how to choose the hook for a publication or edit and check the receiver’s HTTP response.

TL;DR

Use transition_post_status to detect a post becoming public, and post_updated when you need before-and-after fields for an existing post.

  • Register an action callback with the hook’s full argument count.
  • Check the post type and old/new statuses before sending.
  • Inspect both WP_Error and the HTTP response code.

What is a WordPress publish post hook?

A WordPress action hook runs a callback at a defined point in a request. A filter changes a value and returns it to the caller, so it is not the right tool for sending a notification.

Hook parameters are positional. The callback must accept them in the documented order, and the argument count passed to add_action() must include every parameter it uses. A mismatch can leave the callback without required data.

For existing posts, the post_updated hook runs after the database update and passes the post ID plus before-and-after WP_Post objects. Use it to compare edited fields when the status stays publish. For a newly inserted post, choose a hook that handles insertions.

How to send a publish event from WordPress

Inspect the receiver contract before deployment: it must identify the event name, accept the post ID and define how rejected requests are reported.

Step 1: Choose the event and filter the post

This callback treats a transition into publish as a new publication, and an edit that stays published as an update. Change post to your registered custom post type when the receiver should not get ordinary articles.

add_action( 'transition_post_status', 'c4g_notify_post_event', 10, 3 );

function c4g_notify_post_event( $new_status, $old_status, $post ) {
    if ( 'post' !== $post->post_type ) {
        return;
    }

    if ( 'publish' === $new_status && 'publish' !== $old_status ) {
        $event = 'new_article';
    } elseif ( 'publish' === $new_status && 'publish' === $old_status ) {
        $event = 'updated_article';
    } else {
        return;
    }

    c4g_send_post_event( $event, $post->ID );
}

The final 3 tells WordPress to pass all three hook arguments to the callback.

Step 2: Send the event and inspect the result

Keep the payload narrow: an event name and post ID let the receiving service fetch the current data through an authenticated route. Replace the endpoint with your HTTPS receiver URL and configure any secret outside source control.

function c4g_send_post_event( $event, $post_id ) {
    $response = wp_remote_post(
        'https://api.example.com/wordpress/events',
        array(
            'timeout' => 5,
            'headers' => array( 'Content-Type' => 'application/json' ),
            'body' => wp_json_encode(
                array( 'eventType' => $event, 'postId' => (int) $post_id )
            ),
        )
    );

    if ( is_wp_error( $response ) ) {
        error_log( 'Post event request failed: ' . $response->get_error_message() );
        return;
    }

    $status = wp_remote_retrieve_response_code( $response );
    if ( $status < 200 || $status >= 300 ) {
        error_log( 'Post event endpoint returned HTTP ' . $status );
    }
}

wp_remote_post() returns a response array or WP_Error. A response can still carry an unsuccessful HTTP status, so check both.

I could not execute this PHP sample here because the runtime check returned “php: command not found.” The WordPress function names and hook arguments were checked against the official references linked below.

Common errors when sending WordPress publish events

Repeated saves can re-run a callback. A scheduled post also moves from future to publish, so the same transition test catches scheduled publication without a separate timer. Keep the event name tied to the change you mean, not to the editor screen that triggered it.

CaseWhat to checkResponse
Status stays publishSame-status callback may still runSend an update only when intended
Transport failureis_wp_error()Log safely or queue a retry
HTTP error responseResponse code outside 200-299Inspect receiver logs and payload contract
Repeated notificationEditor, plugin or retry may repeat workDeduplicate by event ID or post ID plus version

For duplicate protection, have the receiver store one idempotency key and return the saved result when that same key arrives again. A post ID stays constant across edits, so pair it with a revision identifier if separate edits should create separate events.

When a receiver returns a client error, inspect the endpoint URL, authentication and accepted event names before retrying. A server error can be transient, but a retry should use a bounded policy and the same idempotency key so it does not create duplicate work.

The receiver should authenticate the request and validate the event name and post ID before acting on them. Keep its response contract explicit so WordPress can distinguish accepted work from a rejected request.

The example uses a fixed HTTPS endpoint. If an administrator can change that URL, I would use WordPress’s safe remote-request function because the destination becomes input that needs validation. Keep authentication material out of the post body and logs, and send only fields the receiver needs.

Because a synchronous request holds the WordPress save operation open, set a short timeout and move required retries into a queue worker.

Conclusion: Match the hook to the event

Choose the hook that matches the event your receiver needs. Review the callback conditions before you enable the integration on a site with custom post types.

The official references document the callback arguments for transition_post_status and post_updated. The existing CodeForGeek event-generator plugin is another place to inspect an implementation.

For the next step, package the callback as a WordPress plugin rather than tying it to a theme. The theme-building guide covers the other side of that choice, where presentation code belongs.

FAQ

These answers cover the hook choice and the response check in the example.

Which WordPress hook detects a post becoming published?

Use transition_post_status and check that the new status is publish while the old status was not publish.

Can post_updated detect edits to a published post?

Yes. It runs for an existing post update and passes the post before and after the update. Compare the fields your integration cares about.

Does wp_remote_post() guarantee the receiver processed the event?

No. Check for WP_Error and inspect the HTTP response code. A successful response does not prove downstream work completed.

Pankaj Kumar
Pankaj Kumar

Pankaj Kumar is the founder and CEO of CodeForGeek, with more than 14 years in IT. He is an open-source enthusiast who enjoys sharing what he learns through CodeForGeek and YouTube, with a focus on Python, data analytics, machine learning, Angular, Node.js, and Kafka.

Articles: 337