不得不说,网上关于WordPress中评论回复邮件提醒的功能,相关代码多的可怕,但是但是野人还是偏偏也写,毕竟折腾过的都要一一记录下来,别人的始终是别人的,只要自己实操了去写,去碰到问题,掌握了,才是自己的知识,哪怕是你复制过来的代码,也要动动小手去试试,因为不试不知道,一试吓一跳,卧槽,这么神奇啊,这就成功了啊!!!
所以,快来感受下折腾的乐趣吧,代码如下:
/**
* 评论回复邮件通知
*/
function yr_comment_mail_notify($comment_id)
{
$admin_email = get_bloginfo('admin_email'); // $admin_email 可改指定的 e-mail.
$comment = get_comment($comment_id);
$author = $comment->comment_author;
$content = $comment->comment_content;
$approved = $comment->comment_approved;
$author_email = $comment->comment_author_email;
// 校验是否是个正确的邮箱地址
if (!is_email($author_email)) {
return false;
}
$date = $comment->comment_date;
$parent_id = $comment->comment_parent;
// 存在被回复的父级id和评论的状态是通过的,才进行回复提醒,所以在评论审核通过的时候要记得发送邮件
if ($parent_id !== '0' && $approved === '1') {
$parent_comment = get_comment($parent_id); // 获取被回复人(或叫父级评论)相关信息
$parent_author = $parent_comment->comment_author;
$parent_content = $parent_comment->comment_content;
$parent_post_ID = $parent_comment->comment_post_ID;
$parent_date = $parent_comment->comment_date;
$to = $parent_comment->comment_author_email;
if (!is_email($to)) {
return false;
}
// 回复的不是管理员的邮箱 && 回复和被回复的邮件不能相同
if ($to && $to !== $author_email && $to !== $admin_email) {
$post_ID_title = get_the_title($parent_post_ID);
$blogname = get_option("blogname");
$subject = "{$author}回复了您在文章《{$post_ID_title}》中的留言!";
$message = '
<div style="background-color:#f8f8f8; border:1px solid #D2D1CB; color:#111; -moz-border-radius:8px; -webkit-border-radius:8px; -khtml-border-radius:8px; border-radius:8px; font-size:14px; width:702px; margin:0 auto; margin-top:10px;">
<div style="background:#0e79cc; width:100%; height:60px; color:white; -moz-border-radius:6px 6px 0 0; -webkit-border-radius:6px 6px 0 0; -khtml-border-radius:6px 6px 0 0; border-radius:6px 6px 0 0; ">
<span style="height:60px; line-height:60px; margin-left:30px; font-size:20px;">
您在<strong>《' . $post_ID_title . '》</strong>上的评论有新回复啦!
</span>
</div>
<div style="width:90%; margin:0 auto">
<br />
<p style="font-weight:600;">' . $parent_author . ' <span style="color: blue;">您好!</span></p>
<p>您于 ' . $parent_date . ' 曾在《' . $post_ID_title . '》发表了如下的评论:</p>
<p style="background-color: #fff;border:1px solid #eaeaea;padding: 20px;margin: 15px 0; -moz-border-radius:6px 6px; -webkit-border-radius:6px 6px;; -khtml-border-radius:6px 6px; border-radius:6px 6px;">
' . $parent_content . '
</p>
<p style="font-weight:600;">' . $author . ':</p>
<p>已于 ' . $date . ' 给您做出如下的回复:</p>
<p style="background-color: #fff;border:1px solid #eaeaea;padding: 20px;margin: 15px 0; -moz-border-radius:6px 6px; -webkit-border-radius:6px 6px;; -khtml-border-radius:6px 6px; border-radius:6px 6px;">
' . $content . '
</p>
<p>您可以点击<a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '" style="color:#0e79cc" target="_blank">查看完整内容</a></p><br />
<p style="font-weight:600;">温馨提示:</p>
<p>1.本邮件由系统自动发出,请勿直接回复,谢谢!</p>
</div>
<div style="background:#0e79cc; width:100%; height:30px; color:white; -moz-border-radius:0 0 6px 6px; -webkit-border-radius:0 0 6px 6px;; -khtml-border-radius:0 0 6px 6px; border-radius:0 0 6px 6px; ">
<span style="height:30px; line-height:30px; margin-left:30px; font-size:14px;">
以后有空请多来访,<strong>『' . $blogname . '』</strong>在此谢谢您的支持!
</span>
</div>
</div>';
$headers = "Content-Type: text/html; charset=" . get_option('blog_charset') . "";
@wp_mail($to, $subject, $message, $headers);
}
}
}
add_action('comment_post', 'yr_comment_mail_notify');
/**
* 评论通过审核后发送邮件提醒
*/
function yr_comment_unapproved_to_approved($comment)
{
if (is_email($comment->comment_author_email)) {
yr_comment_mail_notify($comment->comment_post_ID);
}
}
add_action('comment_unapproved_to_approved', 'yr_comment_unapproved_to_approved');
代码虽长,但是不影响它的好使,好用。