Spare 10 Seconds To Avoid Spam Comments Entering Your WordPress

Akismet is the most powerful plugin to separate the genuine comments from those who look like spam. It puts the suspected spam comments in a ‘Spam’ bucket rather than your ‘Pending’ or ‘Approved’ comments queue. But what if you get hundred of suspected comment spam everyday due to spammers and comment posting bots, and now to check whether they are actual spam or some genuine comment is put mistakenly due to some statistics to spam is a real overhead. In this case, the only way left is to delete the comment spam directly without checking the sanity. And may be you could lose some valuable comments also in this case. So, wouldn’t it be better if you let Akismet check the comments for spam only when they come from your own blog post or page i.e. the comment is posted by some individual site visitor and not some program bot.


You can either use a WordPress plugin called ‘Math Comment Spam Protection’ for this purpose. But if you are using WP-Super Cache, Math Comment Spam Protection has some issues that the sum does not match and normal visitors are also not allowed to post comments to your posts.

You may also like to read ‘Your Comments Not Appearing On Other Blogs! Ask Akismet’

Function to avoid WordPress Comment Spam

The easy way out to avoid spam comments entering your WordPress system is, you can just paste the below few lines of PHP code to your theme’s function.php file and you are done 🙂


The wp_get_referer function retrieves/returns the referrer from ‘_wp_http_referer’, HTTP referrer, or based on current page respectively. ‘Referrer’ stand for the address of the page (if any) which referred the user agent/browser to the current page. When the ‘referrer’ for a particular comment posting request is empty, it means that the comment is not posted from your blog page/post (as the address of the page/post which has the comment form would be referrer in this case). And the above function will forbid such comments from being entering to your WordPress blog or site. Thus, those will neither go to Akismet nor go to pending or approval queue.

This function will also reject the comments from the browsers which do not have referrers enabled, but all the browsers today have this feature, so the comments with undefined referrer requests are from nothing but bots.

This function is tied to WordPress action hook check_comment_flood which runs when the comment validation is done that whether this comment is allowed or not. After this, the comment approval status is set.

Please post your valuable suggestions and repercussions to this article in the comments section below.

Approved some of your blog comments in hurry, you may like to read ‘Cleanup Of Comments Section Of Blog Thru Simple Technique

  • Another great Plugin is WP HashCash ( ….

    The Plugin understands a comment made by a human or a Javascript etc BOT. Also prevents most trackback, pingback link.

    So you don’t need askimet at all…..

    The rest of the so called spam is just a link posted by a blogger to his post… which I have no problem of….

  • Amazing ! Didn’t knew of the browser referrer before. Will implement the code. Much much better than the spam protections which kinda confuses the commentator.

  • Thanks this code will help me in controling spam..

  • Oh yes Amit. I implemented this piece of code and removed the “Math Comment System”. After implementing this code, I just get around 3 – 4 spams a day which is manageable. Post your feedback after implementing it on your site.

  • I have no idea! Sanjeev, that’s a good finding! Having to deal with so many spams in a day sometimes I even question akismet, although I know it’s trying its best to capture spams. Will bookmark this and give this a shot.

    Social/Blogging Tracker

  • Thank you for this post. I have been looking for someone to teach me about how to handle spam for two days now and you are the only one I can find doing so. I have two blogs, one is getting a ratio of 24-to-1 comments akismet labels as spam to not. And I do not know how to differentiate spam comments to not.

    I also have been searching for what plugin that was with the math at the comment area. Thank you for that.

    I am sorry your blog is a nofollow, but will continue to visit and learn from your wisdom. There is a great amount of wealth here.

  • Hi Kevin,
    Thanks a lot buddy, I am glad that you liked the post. Here is the details and link about “Math Spam Protection” plugin :

    Please let me know in case of any issue/concern.

  • Pingback: Read Articles on Financial Times Without Registration | WeDuggIt()

  • Dan

    Thanks this was exactly what I have been looking for, do you know if this will be supported in newer versions of wordpress mu?

    Best regards

    Dan, Antivirus Community.