Three tips to quickly block WordPress spam comments

Source: Internet
Author: User
Tags php file

Generally, some functions such as blocking and filtering are added. In fact, they are similar. However, it seems a little cumbersome for some very long ones. Of course, it is not as powerful as the official spam plug-in, but at least it will make you look good. It won't get so much stuff in your database. Let's share some practical little code today, and the results are good. You can also try it.

First, prohibit unauthorized word comments from submitting to the database, put the following code in functions. php of the current topic, and set your blacklist in "discussion" in the background.

// Prohibit spam comments from being submitted to the database function Banfuckspam ($ comment) {if (is_user_logged_in () {return $ comment;} // no pressure on the login user... 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 "); wp_die ('are you familiar with the president in Miguo .. This is Tianchao. Modify the illegal words! ');} Else {return $ comment;} add_filter ('preprocess _ comment', 'banbucketam ');

When any keyword is contained in the comment content, comments name, URL, email, or IP address, the system will prohibit the submission to the database. A line of a keyword or IP address. WordPress also matches the keyword in the word, for example, "press" also matches "WordPress ".

Tip 2: limit the number of WordPress comments. First, put the following code in the functions. php file of the current topic. Note that the minimum number of comments should be modified:

// Limit the minimum number of words for WordPress comments function limit_comment_length ($ commentdata) {$ minlength = 12; // The minimum number of comments preg_match_all ('/. /U', trim ($ commentdata ['Comment _ content']), $ maxlength); $ maxlength = count ($ maxlength [0]); if ($ maxlength <$ minlength) wp_die ('minimum slot needs '. $ minlength.! '); Return $ commentdata;} add_filter ('preprocess _ comment', 'Limit _ comment_length', 8 );

This code also supports Chinese characters. This code can limit the minimum number of characters for WordPress comments.

Of course, if you think too much, you can try the following code. Better results.

// Function limit_comment_length ($ commentdata) {$ minCommentlength = 5; // maximum number of words $ maxCommentlength = 200; // maximum number of words: $ pointCommentlength = mb_strlen ($ commentdata ['Comment _ content'], 'utf8 '); // mb_strlen 1 Chinese character is treated as 1 length if ($ pointCommentlength <$ minCommentlength) {header ("Content-type: text/html; charset = utf-8 "); wp_die ('sorry, your comment is too short. Enter at least '. $ minCommentlength. '(entered '. $ pointCommentlength. 'characters)'); exit;} if ($ pointCommentlength> $ maxCommentlength) {header ("Content-type: text/html; charset = utf-8 "); wp_die ('sorry, your comment is too long, please be less '. $ maxCommentlength. '(entered '. $ pointCommentlength. 'characters)'); exit;} return $ commentdata;} add_filter ('preprocess _ comment', 'Limit _ comment_length ');

With the limit on the number of comments and the keyword blocking, most spam comments can be blocked. You can try it on your own.

What if there are comments? Let's take a look at the ultimate killer!

Third trick: WordPress comment filtering Japanese characters and pure English

Put the code in functions. php of the current topic. The two functions can also be separated and executed. The effect is similar.

// Comment filter function refused_spam_comments ($ comment_data) {$ pattern = '/[1-?] /U'; $ jpattern = '/[response-encoding] + | [response-encoding] +/U'; if (! Preg_match ($ pattern, $ comment_data ['Comment _ content']) {err! You shoshould type some Chinese word! ');} If (preg_match ($ jpattern, $ comment_data ['Comment _ content']) {err ('Japanese rolling rough! Japan Get out! Japan? Outbound bytes! You shoshould type some Chinese word! ');} Return ($ comment_data);} if (dopt ('d _ spamComments_ B') {add_filter ('preprocess _ comment', 'refused _ spam_comments ');}

This code allows the comment to contain only Chinese characters but not Japanese. Of course, if you have business dealings with international friends. Don't do it. If you are using Ajax without refreshing comments, page dislocation may occur. Replace wp_die in the above code with err. The above methods should be used to block the vast majority of spam comments.

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.