Prevent WordPress comment spam

If you have a WordPress website or maintain a blog, you may be aware of the constant barrage of comments which are submitted. Unfortunately, not all of these comments are submitted by humans, there’ll be a quite a lot which come through as spam.

You can install & activate a couple of plugins to help prevent spam but these aren’t always as successful as you want them to be! I’ll name drop a few of these plugins a little later on down the post.

One of the biggest problems we’ve found is on much older sites which have comments disabled, yet spam is still being submitted to the site such a rate that it can’t be deleted quick enough. Not only does this cause a problem with the website as when you log in and see the thousands/hundreds of thousands of comments waiting for your approval but you can start to experience server problems. The problems with the website is pretty obvious – there’s a lot of comment moderation waiting for you, but also if you were thinking about bulk deleting these comments, you should think again – some of it might have actually been left by somebody genuinely commenting on your blog. That means that you could have hours upon hours of filtering through comments to do!  Problems with your server can occur when you are reaching the high end limits of your allowed traffic.

What are my options?

There’s two routes you can take and we’ll quickly cover each of them.

  • Option 1
    • If your website doesn’t allow comments what-so-ever and you’re still getting spammed, we have some useful code which you can add to the theme functions.php or a plugin
  • Option 2
    • If your website does allow comments there’s a few plugins you can install to help.

Option 1

So here’s an option to go absolutely hard-line on comments. You can add the below code to your theme’s functions file.

This code will completely remove all instances of comments from your website. Here’s a bullet point list of everything it does:

  • Disables support for comments & trackbacks in post types
  • Closes comments on the front-end (site-wide)
  • Hides existing comments
  • Remove comments page in backend menu
  • Redirect any user trying to access comments page
  • Remove comments metabox from dashboard
  • Remove comment links from admin bar

One thing you do need to be careful about however, is how old the version is you put it on! The code is tested back to 2.8 but use at your own risk. Here’s the commented code below:


// Disable support for comments and trackbacks in post types
function creare_disable_comments_post_types_support() {
$post_types = get_post_types();
foreach ($post_types as $post_type) {
if(post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
}
add_action('admin_init', 'creare_disable_comments_post_types_support');

// Close comments on the front-end
function creare_disable_comments_status() {
return false;
}
add_filter('comments_open', 'creare_disable_comments_status', 20, 2);
add_filter('pings_open', 'creare_disable_comments_status', 20, 2);

// Hide existing comments
function creare_disable_comments_hide_existing_comments($comments) {
$comments = array();
return $comments;
}
add_filter('comments_array', 'creare_disable_comments_hide_existing_comments', 10, 2);

// Remove comments page in menu
function creare_disable_comments_admin_menu() {
remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'creare_disable_comments_admin_menu');

// Redirect any user trying to access comments page
function creare_disable_comments_admin_menu_redirect() {
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url()); exit;
}
}
add_action('admin_init', 'creare_disable_comments_admin_menu_redirect');

// Remove comments metabox from dashboard
function creare_disable_comments_dashboard() {
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'creare_disable_comments_dashboard');

// Remove comments links from admin bar
function creare_disable_comments_admin_bar() {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
}
add_action('init', 'creare_disable_comments_admin_bar');

Option 2

Obviously the above method can’t be applied if you actually want comments to show on your blog. There are a couple of anti-spam plugins you can install however, to help!

  • Akismet
    • The age old favourite, Akismet has some great stats and really works well at cutting the amount of spam your site may be getting.
  • WP Spam Shield
    • This is quite a good plugin to prevent spam. One of the good things about this plugin is, not only does it prevent comment spam but it can reduce the amount of registration spam you get too (this will only apply to websites which have the ‘anyone can register’ setting ticked).
  • Disqus
    • We use Disuqs here at Creare and it’s the one I’d recommend the most. It takes over the WordPress comment system entirely and really looks good on pretty much any website. The only problem you’ll have here is the backwards compatibility.

I hope some of the above techniques can help you, so happy blogging!