HTML5 Zone is brought to you in partnership with:

My name is Jean-Baptiste Jung and I’m a 29 years old web developer and professional blogger. I was born and raised in Paris, France and I now live in Belgium with my wife and our adorable cat. I first used the internet in 1998, built my first website in 2001 and finally started to work as a professional web developer in 2005. In 2010, I left my job and created my own web development studio. jb is a DZone MVB and is not an employee of DZone and has posted 55 posts at DZone. You can read more from them at their website. View Full User Profile

WordPress hacks and snippets to efficiently reduce spam

06.11.2013
| 3079 views |
  • submit to reddit

Spam is a nuisance and every blogger have to deal with it. As a WordPress user, I bet you already use Akismet, which is definitely a great way to reduce spam. But several other tricks can definitely help. Here are my favorite WordPress hacks and code snippets to reduce spam on your blog.

Automatically mark as spam comments with a super long url

Have you ever noticed that most spam comments have a very long url? So a good way to catch more spam comments which haven’t been caught by Akismet is to automatically mark as spam comments with an url longer than 50 characters.

To apply this tip, just paste the code below in yourfunctions.php file. This code will mark as spam any comment with an url longer than 50 characters. This number can be changed on line 4.

<?php

  function rkv_url_spamcheck( $approved , $commentdata ) {
    return ( strlen( $commentdata['comment_author_url'] ) > 50 ) ? 'spam' : $approved;
  }

  add_filter( 'pre_comment_approved', 'rkv_url_spamcheck', 99, 2 );

?>

This snippet is really efficient and allowed me to reduce spam on my blogs.

Source: http://css-tricks.com/snippets/wordpress/spam-comments-with-very-long-urls/

Remove the url field from your comment form

A radical way to fight spam on your blog is to remove the url field from your comment form, so commenters will not be able to link to their website. It’s not really cool for them, but if you’re running a very large blog with lots of spam, this can be a solution.

Paste this code into your functions.php file. Once saved, the url field will be removed from your comment form.

function remove_comment_fields($fields) {
    unset($fields['url']);
    return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');

Source: http://wp.tutsplus.com/tutorials/creative-coding/customizing-comments-in-wordpress…/

Remove url field from the comment form and automatically spam comments with urls

Now if you want a really really efficient and radical way to get rid of spam, let combine the two functions above.

First, let’s remove the url field from the comment form. And then, make sure that no spammer have a trick to still inserting an url by automatically mark as spam any comment with something in the url field.

function remove_comment_fields($fields) {
    unset($fields['url']);
    return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');

<?php

  function rkv_url_spamcheck( $approved , $commentdata ) {
    return ( strlen( $commentdata['comment_author_url'] ) > 1 ) ? 'spam' : $approved;
  }

  add_filter( 'pre_comment_approved', 'rkv_url_spamcheck', 99, 2 );

?>

This code have to be pasted in your functions.php file to work.

Unlink urls in comment text

Are you tired that some people use your website to link to other sources, most of them being unrelated to the post content? Then here’s a trick I’m using on both CatsWhoCode.com and WPRecipes.com: Remove the automatic linking of url in the comment text.

As usual, this code goes straight to your functions.phpfile.

remove_filter('comment_text', 'make_clickable', 9);

Source: http://www.wprecipes.com/wordpress-hack-remove-autolinks-in-comments

Automatic “Nofollow” for external links in comment text

If you want to reduce the amount of spammy links in comment text, without removing the automatic linking of urls as shown above, here’s an alternative: Simply add arel="nofollow" attribute to all external links.

Paste the code below into your functions.php file, and then save the file.

add_filter('comment_text', 'auto_nofollow');
 
function auto_nofollow($content) {
    //return stripslashes(wp_rel_nofollow($content));
 
    return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content);
}
 
function auto_nofollow_callback($matches) {
    $link = $matches[0];
    $site_link = get_bloginfo('url');
 
    if (strpos($link, 'rel') === false) {
        $link = preg_replace("%(href=S(?!$site_link))%i", 'rel="nofollow" $1', $link);
    } elseif (preg_match("%href=S(?!$site_link)%i", $link)) {
        $link = preg_replace('/rel=S(?!nofollow)S*/i', 'rel="nofollow"', $link);
    }
    return $link;
}

Source: http://www.onextrapixel.com/2012/10/12/5-code-snippets-for-interacting…

Spam comments based on a word list

As spammers generally target specific keywords, it can be useful to create a blacklist and automatically mark as spam comments which contains one or more words from the blacklist.

To add keywords, simply edit the array on line 9. Then, paste this code snippet into your functions.php file.

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']);
		$bad_comment_content = array(
			'viagra', 
			'hydrocodone',
			'hair loss',
			'[url=http', 
			'[link=http', 
			'xanax',
			'tramadol',
			'russian girls',
			'russian brides',
			'lorazepam',
			'adderall',
			'dexadrine',
			'no prescription',
			'oxycontin',
			'without a prescription',
			'sex pics',
			'family incest',
			'online casinos',
			'online dating',
			'cialis',
			'best forex',
			'amoxicillin'
		);
		if (in_comment_post_like($lower_case_comment, $bad_comment_content)) {
			$comment_box_text = wordwrap(trim($post_comment_content), 80, "\n  ", true);
			$txtdrop = fopen('/var/log/httpd/wp_post-logger/nullamatix.com-text-area_dropped.txt', 'a');
			fwrite($txtdrop, "  --------------\n  [COMMENT] = " . $post_comment_content . "\n  --------------\n");
			fwrite($txtdrop, "  [SOURCE_IP] = " . $_SERVER['REMOTE_ADDR'] . " @ " . date("F j, Y, g:i a") . "\n");
			fwrite($txtdrop, "  [USERAGENT] = " . $_SERVER['HTTP_USER_AGENT'] . "\n");
			fwrite($txtdrop, "  [REFERER  ] = " . $_SERVER['HTTP_REFERER'] . "\n");
			fwrite($txtdrop, "  [FILE_NAME] = " . $_SERVER['SCRIPT_NAME'] . " - [REQ_URI] = " . $_SERVER['REQUEST_URI'] . "\n");
			fwrite($txtdrop, '--------------**********------------------'."\n");
			header("HTTP/1.1 406 Not Acceptable");
			header("Status: 406 Not Acceptable");
			header("Connection: Close");
			wp_die( __('bang bang.') );
		}
	}
} 

add_action('init', 'drop_bad_comments');Source: http://www.wprecipes.com/automatically-refuse-spam-comments-on-your-wordpress-blog

Deny commenting to non-referrer requests

Here is a very useful tip to prevent spambots from dropping spam bombs by denying access to all requests that do not originate from your domain.

Copy the code below, update the domain name on line 5 and then paste it in your .htaccess file. This file is located at the root of your WordPress install. Don’t forget to always make a backup before editing this file!

# block comment spam by denying access to no-referrer requests
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
RewriteCond %{HTTP_REFERER} !.*catswhocode.com.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule ^(.*)$ ^http://the-site-where-you-want-to-send-spammers.com/$ [R=301,L]

Source: http://perishablepress.com/block-spam-by-denying-access-to-no-referrer-requests/

Published at DZone with permission of jb j, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)