Preventing comments on the image attachment pages in WordPress

I think Don’t Tread On Me has pretty much lost its meaning these days, especially with regard to spamming. I really hate spam and wish nothing but wicked things to those who do it. In any event, lately I had started seeing spam type comments cropping up on image attachment pages of my site and in checking them out I had no idea that comments could even be placed on these pages. Perhaps I missed that in reading the WordPress Codex at some point or I just plain forgot.

I’m not sure why WordPress does not have a central settings area specifically for controlling comments but it would be nice. Essentially this is left to configuring by way of manual editing of code or using third party plug-ins and I question the necessity of the latter since it seems like a no-brainer to just add the functionality.

The way WordPress handles comments currently allows someone to click on a thumbnail of an image and that in turn displays the image as a post along with a comment area under it. I guess comments on individual images is valuable in some way?

picturespam_40

In any regard, it really drove me nuts whenever I would get a comment notification, review it and find it was on the image page in Russian, jibberish or just nonsense text so I decided to review some of the php files to look for anything related to comments.

I’m currently using the Misty Lake theme by Automattic and noticed in the Image Attachment Template (the image.php file) that towards the bottom it instantiated a call to comments_templates.

mistylake code_60

After making a backup of the image.php file, backups are always recommended, I used the editor in WordPress to removed the line, updated it, and now the comment area no longer appears on image attachment posts.

I will need to remember this moving forward should I ever update the theme as I am sure at some point the image.php file will be updated and I will need to manually remove the line again. Equally, should I change themes I will need to remember to double-check comments. It has actually been a few weeks since I made this manual modification and I have not experienced the spam as I was before. Hopefully the info will help someone.

Well, as an update to this, I decided to create my own custom-functions.php and add a function to do the above.

function DisableMediaComments( $open, $post_id ) {
$post = get_post( $post_id );
if ( 'attachment' == $post->post_type ) {
$open = false;
}
return $open;
}
add_filter( 'comments_open', 'DisableMediaComments', 10, 2 );

The code above should generically work within any theme. There is also another way, based on the comments_template line I removed from the theme I am using. Change the comments_template line from this:

<?php comments_template(); ?>

To this which will only show a Comments form if the parent post has comments enabled:

<?php if (comments_open($post->post_parent)) {
 comments_template();
} ?>

So with the above, if your post is enabled for comments then so will any attached images. Otherwise, if comments are disabled in the post, they will also be disabled for attached images. I believe I Googled the aforementioned and saw it on a few websites/forums so kudos go to whomever posted it first.

In any regard, I still elected for the function in my custom-functions.php file. To me its just easier that way and I don’t have to worry about any code changes to the theme.

Advertisements