Disable Spam Comments in WordPress by Checking Referer


Many wordpress spam comments are generated by bots calling wp_comments.php directly. Therefore, we can add a filter in WordPress functions.php template to avoid spam comments by checking the page referer.

1
2
3
4
5
6
function check_referrer_comment() {
    if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') {
        wp_die(__('Please do not access this file directly.'));
    }
}
add_action('check_comment_flood', 'check_referrer_comment');
function check_referrer_comment() {
	if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') {
		wp_die(__('Please do not access this file directly.'));
	}
}
add_action('check_comment_flood', 'check_referrer_comment');

If the server variable HTTP_REFERER is not set or empty, then the page simply dies before any malicious comments are processed. However, some clever bots might still be able to alter/forge the value, but this simple method should filter most of the spam comments.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
168 words
Last Post: How to Find Maximum Product of Word Lengths?
Next Post: The Weird Thing about Javascript - Part I

The Permanent URL is: Disable Spam Comments in WordPress by Checking Referer

Leave a Reply