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

但是这样设置以后,一旦评论通过审核了,是没有邮件通知评论人的,今天我们就来添加这个通知功能。
代码法
将下面的代码添加到当前主题的 functions.php 即可
/**
* WordPress 评论通过审核后<a href="https://www.wpdaxue.com/tag/%e9%82%ae%e4%bb%b6%e9%80%9a%e7%9f%a5" title="查看与【邮件通知】相关的文章" target="_blank" rel="noopener">邮件通知</a>评论人
* https://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");
}
} |
/**
* WordPress 评论通过审核后<a href="https://www.wpdaxue.com/tag/%e9%82%ae%e4%bb%b6%e9%80%9a%e7%9f%a5" title="查看与【邮件通知】相关的文章" target="_blank" rel="noopener">邮件通知</a>评论人
* https://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 格式,否则可能出现乱码问题。
插件法
如果你觉得修改代码比较麻烦,也可以直接安装 Comment Approved 插件,启用该插件以后,在 设置 – Comment Approved 进入到设置界面:

你可以在后台插件安装界面搜索 Comment Approved 直接安装,或者在这里下载 Comment Approved
来源:
https://www.wpdaxue.com/comment-approved-email.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33101.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57