|
大家都知道 WordPress 黑名单的评论会被自动标记为垃圾评论, 但是垃圾评论还是写入了数据库, 如果集中时间段被大量spam攻击, 这种黑名单机制对保护数据库无济于事。 于是有了下面的代码, 效果是直接拒绝符合黑名单条件的评论发表, 评论不会写入数据库。
1、如没使用 ajax 提交评论的话,下面的代码直接丢主题的 functions.php 里面就可以了。
- function cy_blacklist_spam($comment) {
- if( is_user_logged_in()){ return $comment;} //登录用户无压力...
- $comment['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', "",$_SERVER['REMOTE_ADDR'] );
- $comment['comment_agent'] = isset($_SERVER['HTTP_USER_AGENT'] ) ? substr($_SERVER['HTTP_USER_AGENT'], 0, 254 ) : "";
- 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");
- exit('垃圾评论');
- } else {
- return $comment;
- }
- }
- add_filter('preprocess_comment', 'cy_blacklist_spam');
复制代码 2、如果用了 ajax 提交评论,将下面的代码集成到 ajax 评论判断处即可。- if( wp_blacklist_check($comment_author,$comment_author_email,$comment_author_url, $comment_content )&&!is_user_logged_in()){//登录用户还是无压力...
- err(__('垃圾评论'));
- }
复制代码
|
|