WordPress代碼將垃圾評論拒之門外

垃圾評論,如今可以說是無處不在,作為WordPress使用者的我們,防止垃圾評論的侵擾是必須要做的。之前介紹瞭WordPress防垃圾評論插件:Akismet,今天來介紹使用代碼將垃圾評論拒之門外的方法,目前已經用瞭2年,效果非常棒!

此方法依舊是來自 Willin Kan 大師(可惜他已經退出WordPress圈),折騰很簡單,直接將下面的代碼放到主題的functions.php文件的最後一個 ?>前面即可:

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
// 垃圾評論攔截
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 ( !empty($_POST['w']) && empty($_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 ( !empty($_POST['spam_confirmed']) ) {
			if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment;
			//方法一: 直接擋掉, 將 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();

// 垃圾評論攔截
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 ( !empty($_POST[‘w’]) && empty($_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 ( !empty($_POST[‘spam_confirmed’]) ) {
if ( in_array( $comment[‘comment_type’], array(‘pingback’, ‘trackback’) ) ) return $comment;
//方法一: 直接擋掉, 將 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();

這個方法可以阻止98%以上的垃圾評論,當然瞭,還建議你審核第一次提交的評論人的評論,設置審核後才顯示,如果遇到垃圾評論,將其email、IP、網址等添加到黑名單即可,下面是目前的評論設置。你可以在WP後臺-設置-討論下設置:

wpdaxue.com-201210109

You May Also Like