skip to Main Content

I’m new to WordPress and I have to do the following: on publishing a new article on WordPress I also want to trigger an "event" that take the article data (text, images) and send them to an external API (HTTP post request).

Searching on Google I found some plugin (e.g. wpgetapi.com) that should allow to call an external API but in my understanding it’s more to GET data from an external API to build a WordPress article.
What I would like to have is a place where put some custom logic to trigger during the "publishing event" so that I can popolate the API call using article data.

NOTE: if it’s not possible to intercept the publishing event, it would be ok also to manually push the article data to the external API.

2

Answers


  1. There are a few plugins that can help you to do this, such as:

    • WP Webhooks
    • AutomatorWP

    Once you have installed and activated one of these plugins, you can use it to create a custom logic that is hooked to the publish_post action.


    You could also use something like Zapier, to create a "Zap" that triggers a POST request to an external API when a new article is published


    If you don’t want to use a plugin, you can also add your own custom code to intercept the publishing event and send the article data to the external API.

    Example:

    function send_article_data_to_external_api($post_id) {
      // Get the article data.
      $article_data = array(
        'title' => get_the_title($post_id),
        'content' => get_the_content($post_id),
        'images' => get_attached_media('image', $post_id),
      );
    
      // Make a POST request to the external API.
      // ...
    }
    
    // Hook the function to the `publish_post` action.
    add_action('publish_post', 'send_article_data_to_external_api');
    
    Login or Signup to reply.
  2. You can use Gato GraphQL (free) with the HTTP Client addon (paid): Retrieving data from an external API

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search