[WordPress技术] 直接拒绝 WordPress 黑名单评论的发表

[复制链接]
查看: 396|回复: 0

31

主题

31

帖子

101

积分

注册会员

Rank: 2

积分
101
发表于 2021-1-7 10:03:14 | 显示全部楼层 |阅读模式
大家都知道 WordPress 黑名单的评论会被自动标记为垃圾评论, 但是垃圾评论还是写入了数据库, 如果集中时间段被大量spam攻击, 这种黑名单机制对保护数据库无济于事。 于是有了下面的代码, 效果是直接拒绝符合黑名单条件的评论发表, 评论不会写入数据库。

1、如没使用 ajax 提交评论的话,下面的代码直接丢主题的 functions.php 里面就可以了。
  1. function cy_blacklist_spam($comment) {
  2.     if(  is_user_logged_in()){ return $comment;} //登录用户无压力...
  3.    $comment['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', "",$_SERVER['REMOTE_ADDR'] );
  4.    $comment['comment_agent'] = isset($_SERVER['HTTP_USER_AGENT'] ) ? substr($_SERVER['HTTP_USER_AGENT'], 0, 254 ) : "";
  5.     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'] )){
  6.         header("Content-type: text/html; charset=utf-8");
  7.         exit('垃圾评论');
  8.     }  else  {
  9.         return $comment;
  10.     }
  11. }
  12. add_filter('preprocess_comment', 'cy_blacklist_spam');
复制代码
2、如果用了 ajax 提交评论,将下面的代码集成到 ajax 评论判断处即可。
  1. if( wp_blacklist_check($comment_author,$comment_author_email,$comment_author_url, $comment_content )&&!is_user_logged_in()){//登录用户还是无压力...
  2.         err(__('垃圾评论'));   
  3.     }
复制代码

腾讯云
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩图文



在线客服(工作时间:9:00-22:00)
400-600-6565

内容导航

微信客服

Copyright   ©2015-2019  云服务器社区  Powered by©Discuz!  技术支持:尊托网络     ( 湘ICP备15009499号-1 )