• Hi,

    I’d like to send my wordpress posts by API to another website/portal (not WordPress).

    How do I have to make it? And how can I test it?

    thanks in advice

Viewing 3 replies - 1 through 3 (of 3 total)
  • You would need to know how to interact with the API offered by the website/portal where you want to send your WordPress posts.

    WordPress has hooks where you can plug custom code that would execute when those hooks are executed. You would likely be interested in wp_insert_post hook where you would receive some data along with your hook inside of which you can send the data to the external API.

    add_action('wp_insert_post', 'send_to_external_service', 10, 3);
    
    function send_to_external_service($post_id, $post, $update) {
        // Send to API here
    }
    

    You can create a simple plugin to put that code in.

    WordPress also offer functions to send http request – wp_remote_post()

    Thread Starter lore21

    (@lore21)

    Hi @ashfame thanks for reply.

    I don’t know how to create a plugin, but I think I can put the code in a child functions.php file, right?

    add_action('wp_insert_post', 'send_to_external_service', 10, 3); 
    function send_to_external_service($post_id, $post, $update) 
    {
    
    {
    // swagger API code
    "identifier": "ipsum",
    
    "destinationName": "laborum esse dolor ut quis",
    
    "description": "exercitation et amet",
    
    "language": "it",
    
    "longitude": "in ",
    
    "latitude": "cupidatat ex fugiat dolore",
    
    "image": [
    
    {
    
    "imageLicense": "consequat qui cupidatat",
    etc etc etc...
    }

    is it correct?

    Thanks in advice

    Yes, functions.php would work. Also, you would need to pull out properties from $post object like $post->post_title to make up your data before you send it.

    Available properties on post object:

    object(stdClass)
          public 'ID' => int
          public 'post_author' => string
          public 'post_date' => string
          public 'post_date_gmt' => string
          public 'post_content' => string
          public 'post_title' => string
          public 'post_excerpt' => string
          public 'post_status' => string
          public 'comment_status' => string
          public 'ping_status' => string
          public 'post_password' => string
          public 'post_name' => string
          public 'to_ping' => string
          public 'pinged' => string
          public 'post_modified' => string
          public 'post_modified_gmt' => string
          public 'post_content_filtered' => string
          public 'post_parent' => int
          public 'guid' => string
          public 'menu_order' => int
          public 'post_type' => string
          public 'post_mime_type' => string
          public 'comment_count' => string
          public 'filter' => string

    Do mind that any syntax error in that file and your site would stop loading, so make sure you are able to change it again when you save it i.e. changing that file other than the WP file editor.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘API to send posts’ is closed to new replies.