外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>用户交互>WordPress自动拒绝包含特定关键词的垃圾评论

WordPress自动拒绝包含特定关键词的垃圾评论

针对WordPress站点的垃圾评论依旧如潮水般,昨天倡萌在给自留地更换新版本主题后,忘记开启主题内置的垃圾评论拦截功能,没想到就半个小时,就有几十条垃圾评论!所以,今天的主要话题还是如何防范垃圾评论。

将下面的代码添加到主题的functions.php文件,自己根据需要,修改 $ bad_comment_content 数组的内容,任何包含在$ bad_comment_content 数组内的字符,将会被自动拒绝留言。

function in_comment_post_like($string, $array) {   
    foreach($array as $ref) { if(strstr($string, $ref)) { return true; } }   
    return false;  
}  
function drop_bad_comments() {  
    if (!empty($_POST['comment'])) {  
        $post_comment_content = $_POST['comment'];  
        $lower_case_comment = strtolower($_POST['comment']);  
        $bad_comment_content = array(  
            'viagra',   
            'hydrocodone',  
            'hair loss',  
            'xanax',  
            'tramadol',  
            'russian girls',  
            'russian brides',  
            'lorazepam',  
            'adderall',  
            'dexadrine',  
            'no prescription',  
            'oxycontin',  
            'without a prescription',  
            'sex pics',  
            'family incest',  
            'online casinos',  
            'online dating',  
            'cialis',  
            'best forex',  
            'amoxicillin'  
        );  
        if (in_comment_post_like($lower_case_comment, $bad_comment_content)) {  
            $comment_box_text = wordwrap(trim($post_comment_content), 80, "\n  ", true);  
            $txtdrop = fopen('/var/log/httpd/wp_post-logger/nullamatix.com-text-area_dropped.txt', 'a');  
            fwrite($txtdrop, "  --------------\n  [COMMENT] = " . $post_comment_content . "\n  --------------\n");  
            fwrite($txtdrop, "  [SOURCE_IP] = " . $_SERVER['REMOTE_ADDR'] . " @ " . date("F j, Y, g:i a") . "\n");  
            fwrite($txtdrop, "  [USERAGENT] = " . $_SERVER['HTTP_USER_AGENT'] . "\n");  
            fwrite($txtdrop, "  [REFERER  ] = " . $_SERVER['HTTP_REFERER'] . "\n");  
            fwrite($txtdrop, "  [FILE_NAME] = " . $_SERVER['SCRIPT_NAME'] . " - [REQ_URI] = " . $_SERVER['REQUEST_URI'] . "\n");  
            fwrite($txtdrop, '--------------**********------------------'."\n");  
            header("HTTP/1.1 406 Not Acceptable");  
            header("Status: 406 Not Acceptable");  
            header("Connection: Close");  
            wp_die( __('bang bang.') );  
        }  
    }  
}  
add_action('init', 'drop_bad_comments');  

原文:http://www.wprecipes.com/automatically-refuse-spam-comments-on-your-wordpress-blog

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
用户交互

WordPress联系表单插件:Magic Contact(侧边悬浮收缩展开)

2013-5-19 7:52:26

用户交互

WordPress评论中禁止或必须包含指定内容

2013-5-22 8:01:00

2 条回复 A文章作者 M管理员
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索