Wordpress主题技巧

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

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

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

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

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
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');

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

Wordpress主题技巧

更多有关于 Wordpress主题技巧

V9火车采集器,快速入门!

塔克网整站模板 安装使用指南

WordPress如何备份网站数据库和文件?WordPress整站搬家操作流程。

WordPress网站安全设置教程

国外英文广告联盟网站模板,WordPress主题整站演示地址列表

WordPress主题开发:开启文章缩略图功能

WordPress 自定义头像插件:WP User Avatar

WordPress 后台管理界面UI自定义插件:WP Admin UI Customize

WordPress 自定义文章/页面阅读权限的插件:User Specific Content

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2016 Tadke.com. Powered by Wordpress.