0

In wordpress whenever new blog post is created all post details need to be send to third party api. I'm using save_post hook for this but not sure whether it's getting called or not This is what i've done so far

     add_action( 'save_post', 'new_blog_details_send');
    function new_blog_details_send( $post_id ) {

  //getting blog post details//
        $blog_title = get_the_title( $post_id );
        $blog_link = get_permalink( $post_id ); 
        $blog_text = get_post_field('post_content', $post_id);

        ///Sending data to portal////
        $post_url = 'http://example.com/blog_update';
        $body = array(
                'blog_title' => $blog_title,
                'blog_link' => $blog_link,
                'blog_text' => $blog_text
        );

        //error_log($body);

        $request = new WP_Http();
        $response = $request->post( $post_url, array( 'body' => $body ) );


    }

Not sure how to log or debug in wordpress.Any help would be appericiated Thanks in advance

2

A quick way of debugging the save function before redirection - is to

die(print_r($post_id)); // or var_dump($post_id);

this will stop all php from continuing and is fast for small debugging where you don't need an entire log.

throw that into your function, and see what happens in it - change the variable to see if you are getting what you are expecting.

EDIT -----------

what i mean is :

add_action( 'save_post', 'new_blog_details_send', 10, 1);
function new_blog_details_send( $post_id ) {

    wp_die(print_r($post_id)); //just another way of stopping - for wordpress

  //getting blog post details//
    $blog_title = get_the_title( $post_id );
    $blog_link = get_permalink( $post_id ); 
    $blog_text = get_post_field('post_content', $post_id);

    ///Sending data to portal////
    $post_url = 'http://example.com/blog_update';
    $body = array(
            'blog_title' => $blog_title,
            'blog_link' => $blog_link,
            'blog_text' => $blog_text
    );

    //error_log($body);

    $request = new WP_Http();
    $response = $request->post( $post_url, array( 'body' => $body ) );


}

if it is still running normally after you put this in, the function is not called.

  • still getting nothing.Page is running just the same so does it mean hook is not being called. And it will outout in blog page right – Parth Goswami Apr 12 '17 at 7:24
  • 1
    if your saving and redirect works fine, with a die in the function - then the function is not being called. – Stender Apr 12 '17 at 7:29
  • any ideas what could be wrong? It calls when new post is publish right? I've put it in functions.php file at the end – Parth Goswami Apr 12 '17 at 7:34
  • 1
    you can see all of wordpress action hooks here : codex.wordpress.org/Plugin_API/Action_Reference – Stender Apr 12 '17 at 7:42
  • 1
    no thanks necessary - only upvotes so i can finally start commenting normally instead of having to provide an answer everytime :-) – Stender Apr 12 '17 at 7:47
0

Try adding priority and parameter count to your add_action function.

I sometimes get same issue of hook not running when I don't specify parameters when parameters are used.

add_action( 'save_post', 'new_blog_details_send', 10, 1);

Where 10 is the priority, and 1 is the "accepted arguments" for the function

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.