最近老梁运维博客里好多垃圾评论,这Wordpress里的垃圾评论一直是一顽疾,必须手动禁止一些东西或用一些代码或插件方可完成,独立上使用,没啥球用!

可以用简单的验证码验证评论功能+外加禁止英文等语言评论的方法很好解决这一问题~~

解决方法如下:1.引入下图验证码功能,2.引入禁止全英文和日文评论的代码
将下面代码根据自己的需要添加到当前主题的 functions.php中,然后在需要的评论代码处调用即可。

//自制验证码
function loper_protection_math(){
# 数字加法两个随机数, 范围0~99
$num1=rand(0,9);
$num2=rand(0,9);
echo "<input type=\"text\" name=\"sum\" class=\"text\" value=\"\" size=\"25\" tabindex=\"4\" placeholder=\"$num1 + $num2 = ?\" >\n";
echo "<input type=\"hidden\" name=\"num1\" value=\"$num1\">\n";
echo "<input type=\"hidden\" name=\"num2\" value=\"$num2\">";
echo "<label for=\"math\">请输入(计算结果)</label>\n";
}
function loper_protection_pre($commentdata){
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:
break;case null:err('错误: 请输入验证码。');
break;default:err('错误: 验证码错误。');}
return $commentdata;}
if($comment_data['comment_type']==''){
add_filter('preprocess_comment','loper_protection_pre');}

//禁止全英文和日文评论
function v7v3_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
$jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
$ruattern ='/[А-я]+/u';
$arattern ='/[ - ]+|[ - ]+|[ - ]+/u';
$thattern ='/[ก-๛]+/u';
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
err( "写点汉字吧,博主外语很捉急! Please write some chinese words!" );
}
if(preg_match($jpattern, $incoming_comment['comment_content'])){
err( "日文gun!Japanese G out!日本語出て行け!" );
}
if(preg_match($ruattern, $incoming_comment['comment_content'])){
err( "北方野人讲的话我们不欢迎!Russians, get away!Savage выйти из Русского Севера!" );
}
if(preg_match($arattern, $incoming_comment['comment_content'])){
err( "不要用阿拉伯语!Please do not use Arabic!! " );
}
if(preg_match($thattern, $incoming_comment['comment_content'])){
err( "renyao你好,renyao再见!Please do not use Thai!กรุณาอย่าใช้ภาษาไทย!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'v7v3_comment_post');

在comments.php的form表单中添加以下代码:

<?php loper_protection_math();?>

发表回复

后才能评论