[英]wordpress - wp insert post hook for only new posts saved
I am using wp insert post
as a hook to send an email when ever a new post has been submitted in wordpress, I have taken the reference of this link and tried below code.当在 wordpress 中提交了新帖子时,我使用wp insert post
作为挂钩来发送 email,我参考了此链接并尝试了以下代码。 I am able to get emails perfectly, the problem is I am receiving mails even when the post is trashed which is not required.我能够完美地收到电子邮件,问题是即使帖子被丢弃,我也会收到邮件,这不是必需的。 Is there any way to trigger mail only when new post is created and not for any other actions.有没有办法仅在创建新帖子而不是任何其他操作时触发邮件。
function my_project_updated_send_email( $post_id, $post, $update ) {
// If this is a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
return;
$post_url = get_permalink( $post_id );
$subject = 'A post has been updated';
$message = "A post has been updated on your website:\n\n";
$message .= $post->post_title . ": " . $post_url;
// Send email to admin.
wp_mail( 'admin@example.com', $subject, $message );
}
add_action( 'wp_insert_post', 'my_project_updated_send_email', 10, 3 );
I just figured it out just after posting this question, but updating my solution here so that it would be useful for some one. 我刚在发布此问题后才弄清楚了,但是在这里更新了我的解决方案,以使其对某些人有用。
The solution is I have added a checkpoint by checking the post_status
of submitted post using the same wp insert post
like below. 解决的办法是我使用如下相同的wp insert post
通过检查提交帖子的post_status
添加了一个检查点。
$post_status = get_post($post_id)->post_status;
if($post_status == 'pending'){
//send the post pending email
}elseif($post_status == 'publish'){
// send the post published email
}elseif($post_status == 'trash'){
// send the post trashed email
}
You can use Post Status Transitions . 您可以使用“ 发布状态转换” 。 Example for draft -> publish
below. draft -> publish
示例draft -> publish
下面draft -> publish
。
add_action('draft_to_publish', 'draft_to_publish_actions');
function draft_to_publish_actions($object)
{
//do stuff
}
function wcl_insert_car($post_ID, $post, $update) {
if (get_post_type() != 'car') {
return;
}
$value = get_post_meta($post_ID, 'car_init', true);
if (empty($value)) {
// One time code on init Post
update_post_meta($post_ID, 'car_init', true);
}
}
add_action('wp_insert_post', 'wcl_insert_car', 10, 3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.