admin管理员组

文章数量:1292142

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 = '';
        $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 appreciated. Thanks in advance.

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/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 appreciated. Thanks in advance.

Share Improve this question edited May 19, 2021 at 7:16 xXx 2351 silver badge4 bronze badges asked Apr 12, 2017 at 7:02 Parth GoswamiParth Goswami 1011 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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/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.

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

本文标签: savepost hook is not called when post is saved