Are you fed up of moderating spam comments on your WordPress blog? It’s not rare to find spam comments that are written in a way that they appear as genuine feedback until you read it carefully or check the link. How not to spend time sifting through numerous spam comments, looking for one genuine response?
Though there is Akismet, a WordPress plug-in to stop spammers from flooding your blog, it requires a subscription if it’s not a personal blog. How about a code that will fight spammers by automatically deleting spam comments with certain words or phrases? Just copy and paste the following code in the functions.php
of your theme.
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’]);
// List of banned words in comments.
// Comments with these words will be auto-deleted.
$bad_comment_content = array(
‘viagra’,
‘hydrocodone’,
‘[url=http’,
‘[link=http’,
‘xanax’,
‘tramadol’,
‘lorazepam’,
‘adderall’,
‘dexadrine’,
‘no prescription’,
‘oxycontin’,
‘without a prescription’,
‘sex pics’,
‘family incest’,
‘online casinos’,
‘online dating’,
‘cialis’,
‘amoxicillin’
);
if (in_comment_post_like($lower_case_comment, $bad_comment_content)) {
wp_die( __(‘Darn! Your comment contains banned words.’) );
}
}
}
add_action(‘init’, ‘drop_bad_comments’);
In the $bad_comment_content
array, you can add the words that you find in your spams. Be sure to follow the order in which they have been listed—the word withing single quotes, followed by a comma. The last one in the array doesn’t have the comma.
Update: The code has been cleaned to remove an error.