Combat Image Attachment Spam in WordPress

Published on by David A. Kennedy

Spam comments got you down?

Hey, don’t worry – it happens to every blogger. The Akismet service does wonders, helping combat comment spam in a big way, but there’s one specific use case where I’ve noticed it’s struggled lately: spam on WordPress image attachments.

The Problem

By that, I mean the comments specifically on image posts in WordPress.

WordPress gives you the option to set comments open or closed on image attachments, and it doesn’t look like that will change. That’s awesome, especially if you have a gallery and want to have users be able to comment on individual images. But you may not care about that, and rather have users comment on the post itself. I wanted to just have all comments and pings closed on all image attachments.

A Solution

John P. Bloch posted an excellent solution over on WordPress Answers. That worked great for me. I dropped the code into my site’s functionality plugin, (it will also work in your functions.php file) and bam – comment spam reduced.

But the last part of the code wasn’t updating the database values. See it here:

global $wpdb;
$wpdb->update( $wpdb->posts, array( 'comment_status' => 'closed' ), array( 'post_type' =>; 'attachments', 'comment_status' => 'open' ) );

So instead, I ran two simple SQL queries to close comments and pings on image attachments.

They are:

UPDATE wp_posts SET ping_status = replace(ping_status, 'open', 'closed') WHERE post_type = 'attachment';

UPDATE wp_posts SET comment_status = replace(comment_status, 'open', 'closed') WHERE post_type = 'attachment';

That’s worked for me so far. I wanted to share just in case it helps you solve a similar problem, and reduce your comment spam!


Tagged Web DevelopmentWordPress