[英]WordPress "save_post" call only on save post not on update
I am trying to send post data to third-party services when the post is saved via API.当通过 API 保存帖子时,我试图将帖子数据发送到第三方服务。 I am using hook
add_action( 'save_post', [$this, 'save_post_callback'], 100, 3);
我正在使用钩子
add_action( 'save_post', [$this, 'save_post_callback'], 100, 3);
but this hook seems to be called in update post as well as post-new.php in admin panel.但是这个钩子似乎在更新帖子以及管理面板中的 post-new.php 中被调用。 So to get rid of running this hook in post-new.php, I have checked the
$_POST
request but I am not able to filter the update post since I want to call API only in save the post, not in an update.所以为了摆脱在 post-new.php 中运行这个钩子,我检查了
$_POST
请求,但我无法过滤更新帖子,因为我只想在保存帖子时调用 API,而不是在更新中。
There seems to be the third parameter in callback function $update
but it's not working either.回调函数
$update
似乎有第三个参数,但它也不起作用。 Below is my code that needs to be called only in save a post but it's not working.下面是我只需要在保存帖子时调用的代码,但它不起作用。 Any help would be greatly appreciated.
任何帮助将不胜感激。
function save_post_callback( $post_id, $post, $update ) {
// (!$update) => this doesnot seems to work
if(!empty($_POST) && $post->post_type == "post"){
//run only when save post
}
}
simple way is to check if _wp_http_referer
last part is post-new.php or not.简单的方法是检查
_wp_http_referer
最后一部分是否是post-new.php 。
here is a simple code这是一个简单的代码
function save_post_callback($post_id, $post, $update)
{
// (!$update) => this doesnot seems to work
if ( ! empty($_POST) && $post->post_type == "post" ){
$end = explode('/', $_POST[ '_wp_http_referer' ]);
$end = end($end);
if($end == 'post-new.php'){
//echo 'it is new post';exit();
//do what you want here.
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.