阅读 桌面完整版
博客一天能收到好几十个垃圾评论 目测应该都是些机器人灌水
虽然有一些检测的插件 (例如 Akismet) 但是 这些评论还是会存在数据库中 等待过期自动被删除或者你手动删除, 我有点强迫症 一看这么多非得手动去一个一个点 非常的累
LEONA 提供了一个非常好的解决方案 可惜不支持 pingback, trackback (就是文章引用记录, 我觉得是可以保留的) PING-BACK 的时候 XML-RPC需要打开.
原理很简单 wordpress 提供 preprocess_comment 的过滤 然后你需要对传进来的 评论数据进行判断(如果用户真实评论的话会至少按一键这样就触发onkeyup事件) 如果认为是垃圾评论就 使用 wp_die() 提前结束.
WP 官网说 $comment_data 包括了
1 2 3 4 5 6 7 | 'comment_post_ID' - The post to which the comment will apply 'comment_author' - (may be empty) 'comment_author_email' - (may be empty) 'comment_author_url' - (may be empty) 'comment_content' - The text of the proposed comment 'comment_type' - 'pingback', 'trackback', or empty for regular comments 'user_ID' - (empty if not logged in) |
'comment_post_ID' - The post to which the comment will apply 'comment_author' - (may be empty) 'comment_author_email' - (may be empty) 'comment_author_url' - (may be empty) 'comment_content' - The text of the proposed comment 'comment_type' - 'pingback', 'trackback', or empty for regular comments 'user_ID' - (empty if not logged in)
那么我们就可以丰富这个判断内容 即支持 pingback/trackback 又可以屏蔽垃圾代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | $leonax_magic_lower = 328; $leonax_magic_upper = 3450709; function leonax_anti_spam_form($fields){ global $leonax_magic_lower, $leonax_magic_upper; $leonax_magic = mt_rand($leonax_magic_lower, $leonax_magic_upper); $fields['leonax_magic'] = <<<EOT <input type="hidden" id="leonax-magic" name="leonax-magic" value="0"> <script type="text/javascript"> jQuery(function() { jQuery("#comment").on("keyup", function() { jQuery("#leonax-magic").val("$leonax_magic"); }); }) </script> EOT; return $fields; } add_filter('comment_form_default_fields', 'leonax_anti_spam_form'); function leonax_anit_spam_caught() { wp_die('<strong>ERROR</strong>: Looks like you are a spam bot. Please stop doing this.'); } function leonax_anti_spam_check( $commentdata ) { // ---以下为修改的---- $comment_type = ''; if ( isset($commentdata['comment_type']) ) { $comment_type = trim($commentdata['comment_type']); } if ( ($comment_type == 'pingback') || ($comment_type == 'trackback') ) { return $commentdata; } $content = ''; if ( isset($commentdata['comment_content']) ) { $content = trim($commentdata['comment_content']); } if (!strlen($content)) { leonax_anit_spam_caught(); } if (preg_match("/[a-e0-9]{25,}/i", $content)) { // 人为测试捕捉 上图奇怪的ID leonax_anit_spam_caught(); } // ---修改结束--------- global $leonax_magic_lower, $leonax_magic_upper; if ( isset($commentdata['user_ID']) && $commentdata['user_ID'] ) { // 登陆用户允许 return $commentdata; } if ( !isset($_POST['leonax-magic']) ) { leonax_anit_spam_caught(); } $magic = intval($_POST['leonax-magic']); if ($magic < $leonax_magic_lower || $magic > $leonax_magic_upper) { leonax_anit_spam_caught(); } return $commentdata; } add_filter( 'preprocess_comment' , 'leonax_anti_spam_check' ); |
$leonax_magic_lower = 328; $leonax_magic_upper = 3450709; function leonax_anti_spam_form($fields){ global $leonax_magic_lower, $leonax_magic_upper; $leonax_magic = mt_rand($leonax_magic_lower, $leonax_magic_upper); $fields['leonax_magic'] = <<<EOT <input type="hidden" id="leonax-magic" name="leonax-magic" value="0"> <script type="text/javascript"> jQuery(function() { jQuery("#comment").on("keyup", function() { jQuery("#leonax-magic").val("$leonax_magic"); }); }) </script> EOT; return $fields; } add_filter('comment_form_default_fields', 'leonax_anti_spam_form'); function leonax_anit_spam_caught() { wp_die('<strong>ERROR</strong>: Looks like you are a spam bot. Please stop doing this.'); } function leonax_anti_spam_check( $commentdata ) { // ---以下为修改的---- $comment_type = ''; if ( isset($commentdata['comment_type']) ) { $comment_type = trim($commentdata['comment_type']); } if ( ($comment_type == 'pingback') || ($comment_type == 'trackback') ) { return $commentdata; } $content = ''; if ( isset($commentdata['comment_content']) ) { $content = trim($commentdata['comment_content']); } if (!strlen($content)) { leonax_anit_spam_caught(); } if (preg_match("/[a-e0-9]{25,}/i", $content)) { // 人为测试捕捉 上图奇怪的ID leonax_anit_spam_caught(); } // ---修改结束--------- global $leonax_magic_lower, $leonax_magic_upper; if ( isset($commentdata['user_ID']) && $commentdata['user_ID'] ) { // 登陆用户允许 return $commentdata; } if ( !isset($_POST['leonax-magic']) ) { leonax_anit_spam_caught(); } $magic = intval($_POST['leonax-magic']); if ($magic < $leonax_magic_lower || $magic > $leonax_magic_upper) { leonax_anit_spam_caught(); } return $commentdata; } add_filter( 'preprocess_comment' , 'leonax_anti_spam_check' );
我们测试一下 在不登陆 WP的情况下 输入 评论含有 至少20个小写字母和数字构成的这样标识 就被认为是 垃圾评论 (但是不会出现在数据库里 所以你不会看到在等待审核里 直接就被干掉了)
使用方法就是把上面的代码 复制到 模板函数文件 functions.php (如果有子模板就放在子模板里)
效果非常的好!
英文: https://helloacm.com/the-best-efficient-anti-spam-php-code-detection-for-wordpress/
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK
基本上号称根治垃圾留言的方法,基本上是过度宣传的例子.
谁用谁知道!
打算试试这个方法
效果如何? 是不是棒棒达?
最近自己搞了一个轻巧的方法,能阻止常规的垃圾评论,会有tranckback过来的垃圾评论,不清楚trankback是如何工作的
Your comment is awaiting moderation.
大概你这里也有trankback来的垃圾评论吧
Settings -> Discusssions -> “Allow link notifications from other blogs (pingbacks and trackbacks) on new articles ” 不勾选就可以
Trackback 我在WP给禁掉了
pingback有没有禁止,求分享如何禁trankback的
如果评论提交的是代码这种含有大量字母数字的那就过不去了.
是的, 不过概率太小了
preg_match(“/[a-e0-9]{25,}/i”, $content) 匹配了25个字或更长的 字母或数字.