Comments is important part of sites and blogs running of WordPress platform, as it increases engagement of the visitors, prolong the stay of the visitors on the website while reducing the bounce rate, all important elements for search engine optimization (SEO).

However, opening to comments also invites spam. Nowadays, the spam also targets attachment page, which you don’t have much control over, as WordPress automatically creates a page for each and every media that is been uploaded through WordPress. Newly created attachment pages inherit the global settings of “Settings” -> “Discussion” which applies to all posts and pages in whether to enable or disable the comments.

We have ways to prevent comments to be posted to media page or attachment pages, but if you wish, you can force WordPress to automatically close comments at database level when media is uploaded. If you want to continue to allow commenting to enable and work on your posts and/or pages, but want to disable and close the comments feature on media attachment pages only, insert the following filter into the WordPress active theme’s functions.php file. The code forces all new attachment pages to have the value of “false” for “comment_status” when saving to database, thus preventing future media and attachments from having open comments.

function no_attachment_comments( $data ){
    if( $data['post_type'] == 'attachment' )
        $data['comment_status'] = 'closed';
    return $data;
}

add_filter( 'wp_insert_post_data', 'no_attachment_comments' );

If you prefer, you can put the code above into a plugin and activate it, which should work similarly fine too.