我正在使用wp insert post作为一个钩子发送电子邮件,当一个新的帖子已经提交到wordpress,我已经参考了这个链接,并尝试了下面的代码。我能够完美地收到电子邮件,问题是我收到的邮件,即使是垃圾邮件,这是不必要的。是否有任何方法只在创建新帖子时触发邮件,而不用于任何其他操作。
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 );发布于 2016-06-26 22:42:02
我刚发完这个问题就想出来了,但在这里更新了我的解决方案,这样对某个人是有用的。
解决方案是,我使用如下所示的相同的post_status检查提交的帖子的wp insert post,从而添加了一个检查点。
$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
}发布于 2016-06-26 23:02:14
您可以使用后状态转换。下面是draft -> publish的例子。
add_action('draft_to_publish', 'draft_to_publish_actions');
function draft_to_publish_actions($object)
{
//do stuff
}发布于 2022-09-04 15:15:25
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);https://stackoverflow.com/questions/38043811
复制相似问题