This post was updated 539 days ago and some of the ideas may be out of date.
网站开通评论功能就会有垃圾评论的可能,这样会浪费很多的精力去处理,有些纯英文还有些附带网址,以下内容为大家讲解了如何使用纯代码正则拦截验证垃圾评论,功能可限制评论纯数字/纯英文/纯表情。
教程开始
将代码放到/wp-content/themes/zibll/functions.php文件即可。注意 此方法评论必须带中文 禁止了纯数字、纯字母和纯表情。
//屏蔽纯英文评论
function refused_english_comments($incoming_comment) {
// 获取评论内容
$comment_content = $incoming_comment['comment_content'];
// 去除评论内容中的 [g=xxx] xxx为任意字符串
$comment_content = preg_replace('/\[g=[^\]]*\]/', '', $comment_content);
// 检查评论内容是否为空
if (empty($comment_content)) {
wp_die('{"error":1,"ys":"danger","msg":"评论不能是纯表情内容 <br/>Comments cannot be purely emoji content"}');
}
// 检查评论内容是否包含中文
$pattern = '/[一-龥]/u';
// 禁止全英文评论
if (!preg_match($pattern, $comment_content)) {
wp_die('{"error":1,"ys":"danger","msg":"评论必须包括中文 <br/>Comments must include Chinese"}');
}
return $incoming_comment;
}
add_filter('preprocess_comment', 'refused_english_comments');
参与讨论