Eliminate Comment Spammers [WordPress]


If you are on Self-hosted WordPress, spam comments are very common. Though Akismet smartly filters out spam comments, if you prefer not to allow spammers from commenting, the best solution is to have a verification system similar to CAPTCHA, to verify if it’s a human or a spam-bot.

Related: Block Spammers from Visiting your Site/Blog

Instead of CAPTCHA, a simple solution would be a math-based verification wherein a commenter is asked only to answer simple math questions.

Math Comment Spam Protection plugin is one such plugin by Michael Woehrer. Download and activate the plugin. If you prefer, you can change those Error Messages from the ‘Options’ page (Settings->Math Comment Spam).

The process isn’t over yet, you’ll have to add few lines of code to comments.php

To modify comments.php, browse to /wp-content/themes/<theme-name>/comments.php on your server via FTP/cPanel/SSH. Open comments.php using a text editor (I would suggest Notepad++) and add the following lines of code.

<?php
/****** Math Comment Spam Protection Plugin ******/
if ( function_exists('math_comment_spam_protection') ) {
 $mcsp_info = math_comment_spam_protection();
?>     <p><input type="text" name="mcspvalue" id="mcspvalue" value="" size="22" tabindex="4" />
 <label for="mcspvalue"><small>Spam protection: Sum of <?php echo $mcsp_info['operand1'] . ' + ' . $mcsp_info['operand2'] . ' ?' ?></small></label>
 <input type="hidden" name="mcspinfo" value="<?php echo $mcsp_info['result']; ?>" />
</p>
<?php } // if function_exists... ?>

Although you can place the code before the </form> tag, the best place to have the code is after the URL textbox and before the comment text-area so that it doesn’t look odd. An intermediate HTML designer should have no difficulty in adding. If you are unsure, follow the procedure below. [Click the images below to have a larger view]

Math Comment Spam - PHP Code Placement

In your comments.php, scroll to the <input> tag where you get user’s website. To find it, use the search feature in your text editor and search for “$comment_author_url;” (without the quotes). The image above should help you. I’ve highlighted “$comment_author_url;” and textarea. Now, paste the code between these lines, so that it looks like

Math Comment - Spam PHP Code

Now that you have added, save the file and upload it back to the server (to the same location, of course). This should give you something that looks like:

Math Comment Spam - End Result

If you still have issues adding the code and making it to work, feel free to comment below or contact me and I’ll be pleased to help you. :)