4

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?

2
  • Hi. I don't quite understand what you are looking for. Where should the data be passed to? Can you add a simple example please? – Jack Johansson Jun 9 '17 at 14:00
  • I have a webapp that exposes a REST API. I need Wordpress to call such API when a new post is published and send the relevant information about said post (post title, post body, post permalink, etc). Basically, I need the exact same behaviour that was offered by the HookPress plugin. – Marcello Jun 9 '17 at 15:27
3

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.

1
  • @Marcello You're welcome! Glad it helped you out. – Jack Johansson Jun 12 '17 at 9:15
0

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.

2
  • So, there really is no alternative to writing a new plugin from scratch? Where do I write the code from your example? Could you please add to your example the code to make a POST HTTP request and send some JSON data? That would be really appreciated. Thanks. – Marcello Jun 9 '17 at 15:35
  • 1
    It's possible a plugin exists, but it's pretty likely you'll need to create your own plugin. Save the example code as publish-to-rest.php, upload it into /wp-content/plugins/publish-to-rest/ and activate it. Research your particular API, find out what it takes to make the call, try a few things out, and then if you're stuck on a specific error you can create a new WPSE question. You won't find someone to write it all for free, especially since we don't know what API you're trying to call. – WebElaine Jun 9 '17 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.