WordPress评论通过审核后邮件通知评论者

WordPress自带一项功能,就是访客对文章发表评论后,可以设置:在评论显示之前 须经由管理员审核,这可以避免了一些垃圾评论对网站的影响。对此,有评论等待审核时,WordPress会自动给网站管理员发送一封通知邮件,但是评论被管理员审核通过后,WordPress并不会给评论者发送通知,这会给正常的评论者造成一些困惑。

如何让评论通过审核后,给评论者发送一封通知邮件呢?我们可以在当前主题的functions.php中添加以下PHP代码:

add_action('comment_unapproved_to_approved', 'ludou_comment_approved');
function ludou_comment_approved($comment) {
 if(is_email($comment->comment_author_email)) {
 $post_link = get_permalink($comment->comment_post_ID);

 // 邮件标题,可自行更改
 $title = '您在 [梦飞扬] 的评论已通过审核';

 // 邮件内容,按需更改。如果不懂改,可以给我留言
 $body = '您在梦飞扬《<a href="'.$post_link.'">'.get_the_title($comment->comment_post_ID).'</a>》发表的评论:<br />
 '
.
$comment
->
comment_content
.
'<br /><br />
 已通过管理员审核并显示。<br />
 您可在此查看您的评论:<a href="'
.
get_comment_link
(
$comment
->
comment_ID
)
.
'">前往查看</a>'
;

 @wp_mail($comment->comment_author_email, $title, $body, "Content-Type: text/html; charset=UTF-8");
 }
}
展开评论