How To Disable Comments On WordPress Media Image Page Or Attachments

Hello Friends,Some days ago I was working with a plugin which show images in carousel but when we see images in carousel it was showing comments box for media attachment and for media pages which is really not usable and WordPress not providing any easy way to check or uncheck to enable disable comments on WordPress Media. So to do this we have to use filter which will disable comments for media attachments, page.

We are going to put create one function which called by filter ‘comments_open’ in that function we will check if post type is attachment then it will return false, So it will not show comments for attachment media. Below is code to disable comments on Media attachments or page :

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

You need to put this code in your theme functions.php file and it will disable comments on media. I hope it will help you and save your time. Let me know if you need any help in this code.

I am PHP Freelancer, Hire PHP Developer India and PHP Freelancer India. If you have any projects related to WordPress or PHP you can contact me.