I’ve noticed recently that I’m getting a large amount of comment spam. This isn’t unusual, but what is strange is that most of it appears on the comments under images and attachments. Whilst this makes it easier to spot spam (because I know that anything left under an image is not going to be a useful comment), it’s quite annoying and I wondered if removing the ability to leave a comment under an image would reduce the amount of spam I get. WordPress is set up so that when you click on the thumbnail for an image, it will display the full image as a post, complete with a comment box underneath, like this:

Image displayed as a post with comment box underneath

Unfortunately, there is no general option to turn off comments under images or attachments. The solution I’ve found is to override the code in attachment.php which displays the comment box. Here is how I did this:

  • My custom theme is based on the Thematic theme. The file used to generate the code to display attachments as a post is attachment.php. This file will also generate the code to display images as a post if image.php is missing (which in my case it is). So I copied attachment.php from /wp-content/themes/thematic to /wp-content/themes/mytheme
  • I opened the copied file in Notepad++ and commented out the following line:
    comments_template();
  • I uploaded this file to the webserver and now the comments box no longer appears under attachments or images.

This solution isn’t perfect – as you can see if you click on the image above, underneath it still displays some text suggesting you leave a comment. But the comment box is no longer there which is the main thing. I’ll work on updating this text in future but hopefully I’ll now see a reduction in comment spam on image posts.

10 thoughts on “Prevent comment spam on WordPress image and attachment pages

  1. Hey Keane

    Thanks for writing this – have applied it to my theme as a test and received no image spam – for a twelve hour time period. Deleted it…. and within 8 hours I’d been spammed 48 times (only a new site) .. So thank you very much for your easy to understand instructions! I have mentioned you in a post on my site! Cheers

    1. In my case they are attaching comments to site graphics that happened to be in the media uploads folder, and in one case a particular gallery photo. I can’t seem to duplicate their method, when I click on Edit Post next to the spammed image from the Comments area I see the image, but no comment form.

      These are not images I attached to a post, so there is no Link URL text box to uncheck.

      1. Removing a form only thwarts robots that look for forms. I’m not a spammer but if I was I wouldn’t even depend on forms. I’d write a robot that submits directly to the attachment-comment page and skip the form completely. What you’re describing sounds like this is what’s happening.

        It’s not the form that really does anything. It’s the receiving page. Removing forms isn’t a solid solution even if it works against certain robots.

  2. Thanks for this. I’ve been being hit hard on image spam the last few days. Before this I hadn’t had any spam for months (I only keep comments open for 14 days, and spammers always seem to go for older posts on my not-so-frequently updated blog).

    When I went though and started looking at the individual images that were getting hit, I already had no links on individual images, but all the ones attracting the spam were all in galleries (using Tarski as my theme). If clicked, the images opened up in the attachment page, with the option to comment. I’ve switched this over to the image file. It’s not so pretty, but it doesn’t give them any option to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *