Disable Comments on Media Attachment Pages in WordPress

Updated on March 23, 2022

Dealing with spam comments in WordPress is not a pleasant experience. If you are running a WordPress site that does not require comments, then you can simply disable comments via Settings > Discussion and un-checking "Although this setting will disable comments for future posts or pages, it can still be overridden by individual posts or pages.

My client’s WordPress site was hacked & was flooded with a few thousand spam links. The comments were disabled via Settings, but all the media attachment pages were allowing comments. So even after disabling comments completely, I still noticed new comments being added to the wp_comments table. Fortunately, you can disable comments on Media attachment pages and this tutorial will explain how to do the same.

Here’s the screenshot of the comment form on the media attachment page.

Disable comments media page

How to Disable Comments on Media Attachment Pages

To disable comments on media attachment page, you need to copy and paste the below code in your theme’s functions.php file.

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

Once done, you will see “Comments closed” text at the bottom of the media attachment pages. Read more about comments_open filter.

The same can be achieved using a “Disable comments” plugin, which will disable comments globally on posts, pages, attachments and cannot be overridden by individual posts, pages and attachments.

Was this article helpful?

Related Articles

Leave a Comment