当帖子类型进入待处理状态时,我发送了一封电子邮件。
一切正常,但电子邮件发送两次。
这是我的代码:
function pendingPost($id)
{
$type = get_post_type($id);
$post_user = wp_get_current_user($id);
$edit_link = get_edit_post_link($id);
$to = "test@gmail.com";
$subject = "New Entry needs to be approved";
$message = "
<html>
<head>
<title>Pending notification</title>
</head>
<body>
<p>Hi,</p>
<p>A new entry has been submited.</p>
<p>Title : ".get_the_title($id)."</p>
<p>Type : ".substr($type, 0, -1)."</p>
<p>Author : ".$post_user->display_name."</p>
<br />
<p>Click <a href='".$edit_link."'>here</a> to approve it.</p>
</body>
</html>
";
$headers = array('Content-Type: text/html; charset=UTF-8');
$mail = wp_mail($to, $subject, $message,$headers);
}
add_action('pending_posttype', 'pendingPost', 10, 1);
希望有人可以提供帮助。
提前致谢。
答案 0 :(得分:1)
动作挂钩pending_posttype
可能会被触发两次。使用did_action()
,您可以检查操作是否已被触发。
function pendingPost($id) {
if ( did_action( 'pending_posttype' ) !== 1 ) return;
$type = get_post_type($id);
$post_user = wp_get_current_user($id);
$edit_link = get_edit_post_link($id);
$to = "test@gmail.com";
$subject = "New Entry needs to be approved";
$message = "
<html>
<head>
<title>Pending notification</title>
</head>
<body>
<p>Hi,</p>
<p>A new entry has been submited.</p>
<p>Title : ".get_the_title($id)."</p>
<p>Type : ".substr($type, 0, -1)."</p>
<p>Author : ".$post_user->display_name."</p>
<br />
<p>Click <a href='".$edit_link."'>here</a> to approve it.</p>
</body>
</html>
";
$headers = array('Content-Type: text/html; charset=UTF-8');
$mail = wp_mail($to, $subject, $message,$headers);
}
add_action('pending_posttype', 'pendingPost', 10, 1);