前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress代码实现自动拒绝包含特定关键词的垃圾评论

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

作者头像
WindCoder
发布2018-09-20 16:04:45
5000
发布2018-09-20 16:04:45
举报
文章被收录于专栏:WindCoderWindCoder

经常受到类似的垃圾评论,有点懒得动手删了,于是百度出自动拒绝特定关键字的评论的方法并记于此处。

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

代码语言:javascript
复制
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');

参考资料:

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-08-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档