Spammers, always find a way out to spam your blog. Now that I have disabled comments for the posts, I get at least 10 spam comments on the Media files each day.

So I decided to deal with it and since it’s a technology blog I do not think I should be getting any comment on images. Here’s how you can disable comments on the WordPress media.

Simply copy the following code snippet into your active theme’s functions.php

<?php

//function that does the trick
function disallow_media_comments( $open, $post_id ) {
	$post = get_post( $post_id ); // get the complete post
	
	// we check if the post type is an attachment or you can 
	//further check the post mime-type to allow comments for some
	//attachments, like PDF 
	
	if( $post->post_type == 'attachment' ) { 
		return false; //if it's an attachment return comments are closed
	}
	return $open;
}
add_filter( 'comments_open', 'disallow_media_comments', 10 , 2 ); //hook the filter in

 

Hope this helps!

Stay Digified!!
Sachin Khosla

Share this post: