WordPress 代码添加评论通过审核后邮件通知评论者

为了有效控制垃圾评论,建议大家在后台 设置 - 讨论 中,设置一下评论必须审核后才显示,如下图:

WordPress 代码添加评论通过审核后邮件通知评论者

但是这样设置以后,一旦评论通过审核了,是没有邮件通知评论人的,今天我们就来添加这个通知功能。

添加方法

将下面的代码添加到当前主题的 functions.php 即可

/**
 * WordPress 评论通过审核后邮件通知评论人
 * http://www.wpdaxue.com/comment-approved-email.html
 */
add_action('comment_unapproved_to_approved', 'wpdx_comment_approved');
function wpdx_comment_approved($comment){
    if (is_email($comment->comment_author_email)){
        $post_link = get_permalink($comment->comment_post_ID);
        $title = '您在【' . get_bloginfo('name') . '】的评论已通过审核';
 
        $body = '您在《<a href="' . $post_link . '" target="_blank" >' . get_the_title($comment->comment_post_ID) . '</a>》中发表的评论已通过审核!<br /><br />';
        $body .= '<strong>您的评论:</strong><br />';
        $body .= strip_tags($comment->comment_content) . '<br /><br />';
        $body .= '您可以:<a href="' . get_comment_link($comment->comment_ID) . '" target="_blank">查看您的评论</a>  |  <a href="' . $post_link . '#comments" target="_blank">查看其他评论</a>  |  <a href="' . $post_link . '" target="_blank">再次阅读文章</a><br /><br />';
        $body .= '欢迎再次光临【<a href="' . get_bloginfo('url') . '" target="_blank" title="' . get_bloginfo('description') . '">' . get_bloginfo('name') . '</a>】。';
        $body .= '<br /><br />注:此邮件为系统自动发送,请勿直接回复';
 
        @wp_mail($comment->comment_author_email, $title, $body, "Content-Type: text/html; charset=UTF-8");
    }
}

 

你可以根据自己的需要修改上面的代码,来实现某些样式和内容。注意修改functions.php文件后,要保存为 UTF-8 格式,否则可能出现乱码问题。