点击上方蓝字"优派编程"选择“加为星标”,第一时间关注原创干货
原文地址
https://www.fang1688.cn/study-code/2848.html
非插件屏蔽WORDPRESS垃圾评论的方法
我上编讲到“五种WORDPRESS防止垃圾评论方法-过滤垃圾评论提高WP运行效率”https://www.fang1688.cn/wp/1518.html--------------这篇文章是用插件的方法来解决,接下来我讲的是用非插件的方法。
我的另外一个博客主题不是wp官方主题,是国内的主题插件,故安装不了 Math Captcha插件,故用下面的方法。
最近令我头疼是博客评论区被垃圾评论攻击了。WordPress作为著名的博客程序,很多站长都在使用,而为了防止自己的博客被垃圾评论侵扰,很多站长都安装插件来给评论添加验证码功能,其实,我们可以通过纯代码为WordPress的评论添加验证码功能,以及添加禁止英文等语言评论的代码的方法。
可以参考我的博客网站:http://wp.fang1688.cn
简单的验证码验证评论功能+外加禁止英文等语言评论的方法很好解决这一问题!
这货是国外的服务器机器人写脚本发布大量广告垃圾评论到我后台评论区里面。它的ip不断更换,而且厉害之一单靠验证码评论这个功能不能完全阻止它继续发布垃圾评论,还要加上禁止英文评论的代码!
解决方法如下:
引入上图的验证码功能
引入禁止全英文和日文评论的代码
将下面代码根据自己的需要添加到当前主题的 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();?>
大功告成!如下图:
本期内容就到这里啦~以上内容均可在方包博客「http://fang1688.cn」网站直接搜索名称访问哦。欢迎感兴趣的小伙伴试试,如果本文对您有帮助,也请帮忙点个 赞 + 在看 啦!❤️
欢迎大家加入方包的「优派编程」学习圈子,和多名小伙伴们一起交流学习,向方包 1 对 1 提问、跟着方包做项目、领取大量编程资源等。Q群「1159510820」欢迎想一起学习进步的小伙伴~
另外方包最近开发了一款工具类的小程序「方包工具箱」,功能包括:抖音去水印,天气预报,小说在线免费阅读(内含上万部热门小说),历史今天,生成图片二维码,图片识别文字,ai伪原创文章,文字转语音MP3功能...
送福利!关注下方的公众号:「优派编程」回复「资料」,即可获得软件app下载资源和python、java等编程学习资料!
往期文章:
点击卡片关注「优派编程」
定期分享 it编程干货
⬇️ 点击下方阅读原文直达 方包博客