WordPress的垃圾评论是越来越多,by Willin Kan大师的小墙更是相当的强大!
使用:将代码放入主题的functions.php中即是。
<?php
/* <<小牆>> Anti-spam v1.82 by Willin Kan. 2010/12/16 最新修改 */
//建立
class anti_spam {
function anti_spam() {
if ( !current_user_can('level_0') ) {
add_action('template_redirect', array($this, 'w_tb'), 1);
add_action('init', array($this, 'gate'), 1);
add_action('preprocess_comment', array($this, 'sink'), 1);
}
}
//設欄位
function w_tb() {
if ( is_singular() ) {
ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
"textarea$1name=$2w$3$4/textarea><textarea name=\"comment\" cols=\"100%\" rows=\"4\" style=\"display:none\"></textarea>",$input);') );
}
}
//檢查
function gate() {
if ( !emptyempty($_POST['w']) && emptyempty($_POST['comment']) ) {
$_POST['comment'] = $_POST['w'];
} else {
$request = $_SERVER['REQUEST_URI'];
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '隱瞞';
$IP = isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] . ' (透過代理)' : $_SERVER["REMOTE_ADDR"];
$way = isset($_POST['w']) ? '手動操作' : '未經評論表格';
$spamcom = isset($_POST['comment']) ? $_POST['comment'] : null;
$_POST['spam_confirmed'] = "請求: ". $request. "\n來路: ". $referer. "\nIP: ". $IP. "\n方式: ". $way. "\n內容: ". $spamcom. "\n -- 記錄成功 --";
}
}
//處理
function sink( $comment ) {
if ( !emptyempty($_POST['spam_confirmed']) ) {
if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment; //不管 Trackbacks/Pingbacks
//方法一: 直接擋掉, 將 die(); 前面兩斜線刪除即可.
//die();
//方法二: 標記為 spam, 留在資料庫檢查是否誤判.
add_filter('pre_comment_approved', create_function('', 'return "spam";'));
$comment['comment_content'] = "[ 小牆判斷這是Spam! ]\n". $_POST['spam_confirmed'];
}
return $comment;
}
}
$anti_spam = new anti_spam();
// -- END ----------------------------------------
?>
小牆v1.9
<?php
/* -----------------------------------------------
<;<小牆>> Anti-Spam v1.9 by Willin Kan.
*/
//建立
class anti_spam {
function anti_spam() {
if ( !is_user_logged_in() ) {
add_action('template_redirect', array($this, 'w_tb'), 1);
add_action('pre_comment_on_post', array($this, 'gate'), 1);
add_action('preprocess_comment', array($this, 'sink'), 1);
}
}
//設欄位
function w_tb() {
if ( is_singular() ) {
ob_start(create_function('$input', 'return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
"textarea$1name=$2w$3$4/textarea><textarea name=\"comment\" cols=\"60\" rows=\"4\" style=\"display:none\"></textarea>", $input);') );
}
}
//檢查
function gate() {
( !emptyempty($_POST['w']) &;& emptyempty($_POST['comment']) ) ? $_POST['comment'] = $_POST['w'] : $_POST['spam_confirmed'] = 1;
}
//處理
function sink( $comment ) {
if ( !emptyempty($_POST['spam_confirmed']) ) {
//方法一:直接擋掉, 將 die(); 前面兩斜線刪除即可.
//die();
//方法二:標記為spam, 留在資料庫檢查是否誤判.
add_filter('pre_comment_approved', create_function('', 'return "spam";'));
$comment['comment_content'] = "[ 小牆判斷這是Spam! ]\n" . $comment['comment_content'];
}
return $comment;
}
}
$anti_spam = new anti_spam();
// -- END ----------------------------------------