Prevent comment spam on WordPress image and attachment pages

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.

This entry was posted in Development, WordPress. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

8 Comments

  1. Posted 2nd February, 2012 at 11:03 am | Permalink

    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

  2. Steven
    Posted 8th February, 2012 at 6:49 am | Permalink

    Why the need to edit php, when edit image in post / page & click “None” in the “Link Url” box?

    Much easier

    • Posted 9th February, 2012 at 9:30 am | Permalink

      I never thought about that to be honest! It works, but is there a way to set this as the default for every image that you upload?

    • Posted 19th October, 2012 at 4:02 pm | Permalink

      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.

      • Jim
        Posted 7th April, 2013 at 11:37 am | Permalink

        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.

  3. Posted 6th May, 2012 at 2:55 am | Permalink

    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.

  4. Posted 13th October, 2013 at 12:05 am | Permalink

    I’m going to try selecting none when I upload images first as suggested in the comments and will try your method if it fails.

    Thanks!

2 Trackbacks

  • [...] Using Google, I was able to search for related keywords, such as: WordPress, spam, image, and comment. Most search links pointed to yet more plugins and other tools, so I kept looking for a simple answer, and Eureka, I found this simple fix written back in May 2011 by Keane Ingram. [...]

  • [...] Using Google, I was able to search for related keywords, such as: WordPress, spam, image, and comment. Most search links pointed to yet more plugins and other tools, so I kept looking for a simple answer, and Eureka, I found this simple fix written back in May 2011 by Keane Ingram. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>