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 badges2 Answers
Reset to default 2A 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
版权声明:本文标题:save_post hook is not called when post is saved 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741548962a2384763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论