WordPress 如何阻止垃圾评论?

点击上方蓝字"优派编程"选择“加为星标”,第一时间关注原创干货

原文地址

https://www.fang1688.cn/study-code/2848.html


非插件屏蔽WORDPRESS垃圾评论的方法

我上编讲到“五种WORDPRESS防止垃圾评论方法-过滤垃圾评论提高WP运行效率”https://www.fang1688.cn/wp/1518.html--------------这篇文章是用插件的方法来解决,接下来我讲的是用非插件的方法。

我的另外一个博客主题不是wp官方主题,是国内的主题插件,故安装不了 Math Captcha插件,故用下面的方法。

最近令我头疼是博客评论区被垃圾评论攻击了。WordPress作为著名的博客程序,很多站长都在使用,而为了防止自己的博客被垃圾评论侵扰,很多站长都安装插件来给评论添加验证码功能,其实,我们可以通过纯代码为WordPress的评论添加验证码功能,以及添加禁止英文等语言评论的代码的方法。

d1e885f9bea7765c41942a8f124e68c4.png

aeb581e5eacb4861451a1b0071c7bff4.png

可以参考我的博客网站:http://wp.fang1688.cn

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

这货是国外的服务器机器人写脚本发布大量广告垃圾评论到我后台评论区里面。它的ip不断更换,而且厉害之一单靠验证码评论这个功能不能完全阻止它继续发布垃圾评论,还要加上禁止英文评论的代码!

cc9bdc0f6c83aaaf3756d3391f91549d.png

解决方法如下:

  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( "日文滚粗!Japanese Get 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( "人妖你好,人妖再见!Please do not use Thai!กรุณาอย่าใช้ภาษาไทย!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'v7v3_comment_post');

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

<?php loper_protection_math();?>

大功告成!如下图:

b7ad76adb359e2dfdc0ef8975231f933.png

本期内容就到这里啦~以上内容均可在方包博客「http://fang1688.cn」网站直接搜索名称访问哦。欢迎感兴趣的小伙伴试试,如果本文对您有帮助,也请帮忙点个 赞 + 在看 啦!❤️

欢迎大家加入方包的优派编程学习圈子,和多名小伙伴们一起交流学习,向方包 1 对 1 提问、跟着方包做项目、领取大量编程资源等。Q群「1159510820」欢迎想一起学习进步的小伙伴~

另外方包最近开发了一款工具类的小程序「方包工具箱」,功能包括:抖音去水印,天气预报,小说在线免费阅读(内含上万部热门小说),历史今天,生成图片二维码,图片识别文字,ai伪原创文章,文字转语音MP3功能...

送福利!关注下方的公众号:「优派编程」回复「资料」,即可获得软件app下载资源和python、java等编程学习资料!

4df781f0c5e65581ce7837fb68430c8f.png

往期文章:

点击卡片关注「优派编程」
定期分享 it编程干货

‍‍‍ ⬇️ 点击下方阅读原文直达 方包博客   

WordPress屏蔽国外垃圾留言评论,将文件里的代码放到当前主题的 functions.php 中; 的前面就可以了。代码参考:https://boke112.com/bkwd/4209.html 和https://loomob.com/57.html 将以下代码放到当前主题的 functions.php 文件最后一个 ?> 的前面: //防国外灌水 function scp_comment_post( $incoming_comment ) { // 禁止全英文评论 $pattern = '/[x7f-xff]/'; if(!preg_match($pattern, $incoming_comment['comment_content'])) { wp_die( "您的评论中必须包含汉字! <br /> You should type some Chinese word (like "你好") in your comment to pass the spam-check, thanks for your patience! " ); } //禁止 A 链接 if(strstr($incoming_comment['comment_content'], "<a")){ wp_die( "您的评论中不能有 A 链接,请直接填写 URL 地址" ); } // 判断 中文字符占比 $len_all = strlen($incoming_comment['comment_content']); $len_st = mb_strlen($incoming_comment['comment_content'], 'UTF-8'); if(($len_all-$len_st)/(2*$len_st) < 0.5){ wp_die( "中文字符少于百分之五十" ); } return( $incoming_comment ); } add_filter('preprocess_comment', 'scp_comment_post'); //屏蔽关键词,email,url,ip function Shield_fuckspam($comment) { if (wp_blacklist_check($comment['comment_author'], $comment['comment_author_email'], $comment['comment_author_url'], $comment['comment_content'], $comment['comment_author_IP'], $comment['comment_agent'])) { header("Content-type: text/html; charset=utf-8"); err(__('不好意思,您的评论违反画里网站评论规则')); } else { return $comment; } } add_filter('preprocess_comment', 'Shield_fuckspam'); //过滤外文评论 function refused_spam_comments($comment_data) { $pattern = '/[一-龥]/u'; $jpattern = '/[ぁ-ん]+|[ァ-ヴ]+/u'; if (!preg_match($pattern, $comment_data['comment_content'])) { err(__('来一波汉字吧,博主只认识汉字!You should type some Chinese word!')); } if (preg_match($jpattern, $comment_data['comment_content'])) { err(__('原谅博主吧,只听得懂岛国神片的一两句雅蠛蝶 Japanese Get out!日本语出て行け! You should type some Chinese word!')); } return ($comment_data); } add_filter('preprocess_comment', 'refused_spam_comments'); //屏蔽带连接的 function Shield_link($comment_data) { $links = '/http:\/\/|https:\/\/|www\./u'; if (preg_match($links, $comment_data['comment_author']) || preg_match($links, $comment_data['comment_content'])) { err(__('别啊,昵称和评论里面添加链接会怀孕的哟!!')); } return ($comment_data); } add_filter('preprocess_comment', 'Shield_link');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值