現在大量的機器可以直接對WordPress博客進行評論并帶有大量的連接,這些帶鏈接評論我們定為垃圾評論了,下面我來給大家介紹幾款自動將包含長鏈接的評論標記為垃圾評論實例.
方法一:此方法依舊是來自 Willin Kan 大師(可惜他已經退出WordPress圈),折騰很簡單,直接將下面的代碼放到主題的functions.php文件的最后一個 ?>前面即可,代碼如下:
- // 垃圾評論攔截
- 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;
- //方法一: 直接擋掉, ? die(); 前面兩斜線?h除即可.
- 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后臺-設置-討論下設置.
方法二:自動拒絕包含特定關鍵詞的垃圾評論,將下面的代碼添加到主題的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 (!emptyempty($_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');
今天再補充下,自動將包含長鏈接的評論標記為垃圾評論,將下面的代碼添加到主題的 functions.php 文件即可:
- function rkv_url_spamcheck( $approved , $commentdata ) {
- return ( strlen( $commentdata['comment_author_url'] ) > 50 ) ? 'spam' : $approved;
- }
- add_filter( 'pre_comment_approved', 'rkv_url_spamcheck', 99, 2 );
注意看第二行的 50,根據自己的需要修改這個數值,如果你希望將所有帶有鏈接(不管是否是長鏈接)的評論內容都自動標記為垃圾評論,將 50 改為 1 即可.
新聞熱點
疑難解答
圖片精選