Php wordpress-wp仅为保存的新帖子插入帖子挂钩
我正在使用Php wordpress-wp仅为保存的新帖子插入帖子挂钩,php,wordpress,email,Php,Wordpress,Email,我正在使用wp insert post作为一个钩子,在wordpress中提交新帖子时发送电子邮件,我参考了这篇文章并尝试了以下代码。我能够完美地收到电子邮件,问题是我收到的邮件,即使邮件是垃圾,这是不必要的。是否有任何方法仅在创建新帖子时触发邮件,而不用于任何其他操作 function my_project_updated_send_email( $post_id, $post, $update ) { // If this is a revision, don't send the ema
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 );
我只是在发布了这个问题后才想出来,但在这里更新了我的解决方案,以便对某些人有用 解决方案是,我通过使用相同的
wp insert post
检查已提交帖子的post\u状态,添加了一个检查点,如下所示
$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
}
你可以用。下面的draft->publish
示例
add_action('draft_to_publish', 'draft_to_publish_actions');
function draft_to_publish_actions($object)
{
//do stuff
}