我为项目创建了自定义帖子类型,目标是用户可以在后端 wp 管理区域输入项目。发生这种情况时,应向管理员发送一封电子邮件,以便他检查帖子然后发布。
为了实现这一点,我在“wp_insert_post”上添加了一个动作挂钩。我的问题是管理员通知电子邮件发送了 3 次,而不是 1 次。所以我想知道,我的代码有什么问题?
function wgoe_send_admin_notification( $post_id, $post, $update ) {
// If the post type is not a project, don't send the email
if (get_post_type($post_id) != 'project') return;
// If this is a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) )
return;
// If post is being published, don't send the email
if (get_post_status( $post_id ) == 'publish' ) return;
$post_url = get_edit_post_link( $post_id );
$subject = 'A project has been added or updated';
$message = "Howdy Admin! \n\n\n";
$message .= "A user has added or updated a project on your website. Please check the project for publication:\n\n";
$message .= $post->post_title . ": " . $post_url . "\n\n\n";
$message .= "Dive Business For Sale";
// Send email to admin.
$adminemail = get_option( 'admin_email' );
wp_mail( $adminemail, $subject, $message );
}
enter code here
add_action( 'wp_insert_post', 'wgoe_send_admin_notification', 10, 3 );