Posted in Tutorials, WordPress by
Anthony Hortin
(Updated: October 12, 2012)

One of the benefits of having a blog, no matter whether it's WordPress or something else, is being able to interact with your readers through the use of Comments. Obviously, the downside to this is the Spam that you also attract. Thankfully there's quite a few plugins around now that allow you to automatically filter out the majority of Spam comments.

The problem…

Recently though, I found out that within WordPress you can also leave comments on images as well as Posts. It came to my attention through a test blog that I have setup. I use this test site as a “staging server” when I'm making the final touches to a clients website, prior to cutting it across to their “live” domain. It allows me to show the client the finished product with all their content and allows them to check for any last minute revisions. Most of the time Comments are normally Disabled through the Dashboard Settings option (Settings > Discussion > Allow people to post comments on new articles) however recently I started receiving Spam Comments on images themselves. This was happening even though Comments were disabled. It seems that even if your WordPress blog is configured to not allow comments, depending on your theme, it may still be possible for someone to post comments via one of your images.

The solution…

To get around this, you will need to create an attachment.php file for your theme, if it doesn't already have one. If one does exist, WordPress will use your attachment.php file when viewing a single attachment, such as an image. Since all themes are different, I'll show you how to make the modifications to the standard Twenty Ten theme that comes bundled with WordPress.

At around Line 111 in the attachment.php file that is included in the Twenty Ten theme, you should see a line that calls the comments template. It will look like this…

<?php comments_template(); ?>

To avoid having comments posted on your images when you've disabled them, simply replace this line with the following…

<?php if (comments_open($post->post_parent)) {
   // Only show the Comments Form if the parent post has comments open
   comments_template();
} ?>

The If statement in the first line checks the Parent Post to see if comments are allowed and if so, will then call the comments template. If comments aren't allowed, the comments form wont be displayed. The second line is simply a std PHP code comment and can be removed if you'd prefer. Personally though, I like to keep my code well documented.

Of course, if you'd rather not have comments posted on images (or any attachments), then you could also simply comment out or remove the call to the comments_template() function all together, from your attachment.php file.

If your theme doesn't have an attachment.php file, you will need to create one. The simplest way to do this is to start by duplicating (& renaming) your single.php file and then adding in the extra functionality as per the attachment.php file included in the Twenty Ten theme.

I know that I'll be including an attachment.php file in any themes that I create from now on. Leave a comment and let me know if this has helped you. πŸ™‚

15 responses on “Modifying your WordPress Theme to disable image comments

  1. Jahangir

    On my blog I have changed that “” line to the one you mentioned but still I am getting spam comments posted on images πŸ™

    Do you have some other solution?

  2. Tom Auger

    Nice one. There’s a Trac bug request to have this feature looked at, but it’s been pushed back post 3.0. Your workaround is a good solution in the meantime. Thanks for taking the time to write a detailed tutorial.

    1. Leefe

      @Tom Auger
      Nice to see that someone has put this in as a big in the WordPress Trac. I too get lots of spam on images and would love to turn off comments on images altogether (without having to hack my theme).

  3. stuart

    great, hopefully I can stop the spam images too, and looking forward to it bieng standard in wp , the more control the better

  4. Cindi

    Thank you so much, at last a post with common sense information and a piece of code I could use quickly to solve a problem. Mwah!