WordPress stops spam comments

Source: Internet
Author: User
Tags comments

Today, Yifan Qihang will introduce two methods to block spam comments outside the door.

In general, we will use the famous anti-spam comment plug-in: Akismet to prevent spam comments from being infiltrated. Akismet will separate all identified spam comments, it also provides the ability to clean up spam comments with one click. Although Akismet can recognize spam comments by almost 100%, it is not a solution to "clean up with one click" every day because there are too many spam comments, is there a more practical solution to intercept spam comments before submission?

 
Let's first analyze the type of spam comment:

Spam comments in English only or in other languages such as Japanese and Korean
Fix spam comments used by a website for promotion
For these two types of spam comments, we use two methods to block these two types of spam comments.
 

Method 1: Do not submit comments in English only or in other languages including Japanese and Korean

Insert the following code into the appropriate location of the topic directory functions. php:

The code is as follows: Copy code
// Disable all comments in English and Japanese
Function v7v3_comment_post ($ incoming_comment ){
$ Pattern = '/[1-?] /U ';
$ Jpattern = '/[Upload-train] + | [Upload-train] +/U ';
If (! Preg_match ($ pattern, $ incoming_comment ['Comment _ content']) {
Err! Please write some chinese words! ");
    }
If (preg_match ($ jpattern, $ incoming_comment ['Comment _ content']) {
Err ("Rolling in Japanese! Japan Get out! Japan? Outbound bytes! ");
    }
Return ($ incoming_comment );
}
Add_filter ('preprocess _ comment', 'v7v3 _ comment_post ');

Tip: If an exception occurs when you use the above code, replace err of lines 6th and lines 9th with wpdie.

 

Method 2: prevent comments containing prohibited keywords from submitting

Insert the following code into the appropriate location of the topic directory functions. php:

The code is as follows: Copy code

// Prohibit the submission of comments containing prohibited keywords to the database
Function v7v3_fuckspam ($ comment)
{
If (is_user_logged_in ())
    {
Return $ comment;
    } 
If (wp_blacklist_check (
$ Comment ['Comment _ author'],
$ Comment ['Comment _ author_email '],
$ Comment ['Comment _ author_url '],
$ Comment ['Comment _ content'],
$ Comment ['Comment _ author_IP '],
$ Comment ['Comment _ agent']
))
    {
Header ("Content-type: text/html; charset = utf-8 ");
Err ('
Your comment contains forbidden keywords, or your IP address has been blacklisted. If you have any questions, contact the administrator!
');
    } 
Else
    {
Return $ comment;
    }
}
Add_filter ('preprocess _ comment', 'v7v3 _ fuckspam ');

Tip: If an exception occurs when you use the above code, replace err of lines 6th and lines 9th with wpdie.

Set the Prohibited keywords in "dashboard -- settings -- discussion -- comment blacklist". A prohibited keyword occupies one row.


This method is still from master Willin Kan (unfortunately, he has exited the WordPress circle), and it is very easy to toss. Put the following code directly to the last one in the topic's functions. Php file?> Above

The code is as follows: Copy code


/Spam comment interception
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 $1 name = $2 w $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']: 'concealed ';
$ IP = isset ($ _ SERVER ["HTTP_X_FORWARDED_FOR"])? $ _ SERVER ["HTTP_X_FORWARDED_FOR"]. '(via proxy)': $ _ SERVER ["REMOTE_ADDR"];
$ Way = isset ($ _ POST ['w'])? 'Manual operation': 'Comment-free table ';
$ Spamcom = isset ($ _ POST ['comment'])? $ _ POST ['comment']: null;
$ _ POST ['spam _ confirmed '] = "request :". $ request. "n road :". $ referer. "nIP :". $ IP. "n mode :". $ way. "n? Hot? ". $ Spamcom." n -- record successful --";
  }
 }
Function sink ($ comment ){
If (! Empty ($ _ POST ['spam _ confirmed ']) {
If (in_array ($ comment ['Comment _ type'], array ('pingback', 'trackback') return $ comment;
// Method 1: block directly ,? Die (); what are the first two diagonal lines? Divide h.
Die ();
// Method 2: mark it as spam and leave it in the database to check for false positives.
// Add_filter ('pre _ comment_approved ', create_function ('', 'Return" spam ";'));
// $ Comment ['Comment _ content'] = "[small wall determines this is Spam! ] N ". $ _ POST ['spam _ confirmed '];
  }
Return $ comment;
 }
}
$ Anti_spam = new anti_spam ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.