我使用 wp insert post
作为钩子(Hook),在 WordPress 中提交新帖子时发送电子邮件,我已引用此 link并尝试了下面的代码。我能够完美地收到电子邮件,问题是即使帖子被丢弃,我仍然收到邮件,这不是必需的。有没有办法仅在创建新帖子时触发邮件,而不是进行任何其他操作。
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( '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0766636a6e6947627f666a776b622964686a" rel="noreferrer noopener nofollow">[email protected]</a>', $subject, $message );
}
add_action( 'wp_insert_post', 'my_project_updated_send_email', 10, 3 );
最佳答案
您可以使用Post Status Transitions 。下面的草稿 -> 发布
示例。
add_action('draft_to_publish', 'draft_to_publish_actions');
function draft_to_publish_actions($object)
{
//do stuff
}
关于php - wordpress - wp 插入帖子 Hook 仅用于保存的新帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38043811/