(+84) 463.28.7979

Automatically Delete Spam Comments in WordPress

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.

1 function in_comment_post_like($string, $array) {
2 foreach($array as $ref) { if(strstr($string, $ref)) { returntrue; } }
3 return false;
4 }
5 function drop_bad_comments() {
6 if (!empty($_POST['comment'])) {
7 $post_comment_content = $_POST['comment'];
8 $lower_case_comment = strtolower($_POST['comment']);
10 // List of banned words in comments.
11 // Comments with these words will be auto-deleted.
12 $bad_comment_content = array(
13 'viagra',
14 'hydrocodone',
15 '[url=http',
16 '[link=http',
17 'xanax',
18 'tramadol',
19 'lorazepam',
20 'adderall',
21 'dexadrine',
22 'no prescription',
23 'oxycontin',
24 'without a prescription',
25 'sex pics',
26 'family incest',
27 'online casinos',
28 'online dating',
29 'cialis',
30 'amoxicillin'
31 );
33 if (in_comment_post_like($lower_case_comment,$bad_comment_content)) {
34 wp_die( __('Darn! Your comment contains banned words.') );
35 }
36 }
37 }
38 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>