保存帖子时不会调用save_post钩子

时间:2017-04-12 06:24:09

标签: php wordpress api

在Wordpress中创建新博客帖子时,所有帖子详细信息都需要发送给第三方API。我为此使用 save_post 挂钩但不确定是否要调用它 这是我到目前为止所做的事情

     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 ) );


    }

不确定如何在wordpress中记录或调试。任何帮助都会被提前感谢

1 个答案:

答案 0 :(得分:2)

通过为 save_post 采用替代钩子来解决问题。使用 publish_post 代替&具有更高优先级&它起作用了

    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 ) );

 }
add_action( 'publish_post', 'new_blog_details_send',10,1);