WordPress automatically marks comments that contain long links as spam comments

Source: Internet
Author: User
Tags comments php file

Method One

This method is still from Willin Kan (unfortunately he has been out of the WordPress circle), toss is very simple, directly put the following code to the theme of the functions.php file in front of the last?> can:

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$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 ']: ' concealment ';
$IP = Isset ($_server["http_x_forwarded_for"])? $_server["Http_x_forwarded_for"]. ' (through proxy) ': $_server["REMOTE_ADDR"];
$way = Isset ($_post[' W '])? ' Manual operation ': ' without comment form ';
$spamcom = isset ($_post[' comment '))? $_post[' comment ']: null;
$_post[' spam_confirmed ' = "Request:". $request. "N antecedents:". $referer. "NIP:". $IP. "N Way:". $way. "N? Hot?". $spamcom. "N--record success-";
}
}
function Sink ($comment) {
if (!empty ($_post[' spam_confirmed ')) {
if (In_array ($comment [' Comment_type '], array (' Pingback ', ' trackback ')) return $comment;
Method One: Direct block off,? Die (); Front two slash? h can be removed.
Die ();
Method Two: Mark as spam, stay in the database check whether misjudged.
Add_filter (' pre_comment_approved ', create_function (', ' return ' spam; '));
$comment [' comment_content '] = "[small wall judge this is spam!] N ". $_post[' spam_confirmed '];
}
return $comment;
}
}
$anti _spam = new Anti_spam ();

This method can prevent more than 98% of spam comments, of course, the initiative also recommended that you review the first submitted comments, set up after the audit to show, if you encounter spam comments, the email, IP, URL, etc. to add to the blacklist, the following is the current comment on the setting. You can set up in WP background-settings-discussion:


Method Two

Automatically reject spam comments that contain specific keywords

Add the following code to the subject's functions.php file, modify the contents of the $ bad_comment_content array as needed, and any characters contained within the $ bad_comment_content array will automatically reject the message

The code is as follows Copy Code

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), "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 ');

Add today, automatically mark comments that contain long links as spam comments.

Add the following code to the functions.php file for the topic:

The code is as follows Copy Code

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);

Pay attention to the second line of 50, modify the value according to your needs. If you want to automatically mark all comments with links (whether or not long links) as spam comments, change 50 to 1.

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.