Wordpress自動將包含長連結的評論標記為垃圾評論

來源:互聯網
上載者:User

方法一

此方法依舊是來自 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 ( !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(); 前面兩斜線?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 (!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');

今天再補充下,自動將包含長連結的評論標記為垃圾評論。

將下面的代碼添加到主題的 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 即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.