How to call a REST endpoint when a post is published?

The question:

I need to make WordPress call a specific REST endpoint when a new post is published, passing the most important post data via JSON.

I found a plugin, HookPress, that apparently did just that by letting you configure webhooks for various events. Unfortunately it has not been updated in over 2 years and does not work with recent versions of WordPress (>4.6).

Is there any way I can achieve this?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You don’t have to write a new plugin. You can either add the code to your theme’s functions.php file, or create a child theme.

To wrap your data in a JSON format, you can use the json_encode function. Hook into the post when it’s published, and send the data. In the following function, i will send the post’s title, excerpt and featured image URL to the endpoint.

add_action('publish_post', 'call_the_endpoint',10,2);
function call_the_endpoint($post_id, $post){
    // Define an empty array
    $data = array();
    // Store the title into the array
    $data['title'] = get_the_title();
    // If there is a post thumbnail, get the link
    if (has_post_thumbnail()) {
        $data['thumbnail'] = get_the_post_thumbnail_url( get_the_ID(),'thumbnail' );
    }
    // Get the excerpt and save it into the array
    $data['excerpt'] = get_the_excerpt();
    // Encode the data to be sent
    $json_data = json_encode($data);
    // Initiate the cURL
    $url = curl_init('YOUR API URL HERE');
    curl_setopt($url, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($url, CURLOPT_POSTFIELDS, $json_data);
    curl_setopt($url, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($url, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($json_data))
    );
    // The results of our request, to use later if we want.
    $result = curl_exec($url);
}

It would be possible to write an accurate answer if you could provide more information about your API and how you interact with it. However this was a simple example for you to know how to use the publish_post hook to achieve what you are looking for.

Method 2

You can use a post status transition hook. In this case, it sounds like {status}_{post_type} may be the most fitting. Assuming you’re talking about Posts:

<?php
/* Plugin Name: Publish to REST
Description: Whenever a Post is published, WP will call a REST endpoint.
*/
add_action('publish_post', 'wpse_send_rest_data', 10, 2);
function wpse_send_rest_data($ID, $post) {
    // Add your code to call the REST endpoint here.
    // The $post object is available so you can send post details.
    // Example: $post->post_title will give you the title.
    // $post->post_excerpt will give you an excerpt.
    // get_permalink($post) will give you the permalink.
}
?>

In this case, any time a post of type “Post” transitions to “Publish” status (could be brand-new, or an update, or a scheduled post) your custom function will be executed. This type of code is probably most suited to a custom plugin, since presumably even if you change the theme at some point you’ll still want to make your custom REST call.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment