WordPress basically provides two basic built-in ways to disable the comments on media or attachment pages, one which mentioned above.
Method 1: Disable Comments BEFORE Uploading Media
When you disable comments on all new posts and pages, the newly uploaded media will inherit the settings too, unless overridden in individual post level.
- Go to Settings -> Discussion.
- Uncheck the Allow people to post comments on new articles checkbox under “Default article settings” section.
You may also want to deselect the Allow link notifications from other blogs (pingbacks and trackbacks) option.
- Click Save Changes when done.
- Upload your media.
- Re-enable comments for new posts if you wish.
Method 2: Close Comments on Each Media or Attachment Pages Individually
It’s possible to go to post details page of each media or attachment, and disable their comments and/or trackbacks and pingbacks feature.
- Go to Media -> Library.
- Open each media, and then go to Edit more details link.
- Deselect the Allow comments under “Discussion” section, just like a standard posts or pages page in WordPress.
You may also want to disable Allow trackbacks and pingbacks on this page.
NoteIf you don’t see the Discussion section, click on Screen Options below the Toolbar to expand the options, then select Discussion. - Click on Update when done.
Method 3: Through WordPress API Hooks (Actions and Filters) or Plugins
If you have hundreds or thousands of media uploaded with comments opened, unfortunately, WordPress does not provide an easy way to close the comments on media attachments page for existing media. However, WordPress API provides hooks which can be used to close and disable the comments features on all media pages and attachments globally and automatically.
Example of code that does not actually work:
function disable_media_comment( $open, $post_id ) {
if( get_post_type( $post_id ) == 'attachment' ) {
return false;
}
return $open;
}
add_filter( 'comments_open', 'disable_media_comment', 10 , 2 );
In the active theme’s functions.php, add in the following function:
function disable_media_comments( $post_id ) { if( get_post_type( $post_id ) == 'attachment' ) { wp_die("Comment not allowed."); } return $open; } add_action( 'pre_comment_on_post', 'disable_media_comments' );
Alternatively, you can modify the database so that the “comment_status” for attachment post type become “closed”. To do so, you can run the following SQL query in phpMyAdmin or MySQL client on your WordPress database:
UPDATE `wp_posts` SET `comment_status` = 'closed' WHERE `post_type` = 'attachment' AND `comment_status` = 'open';
If you want to disable pingbacks and trackbacks too:
UPDATE `wp_posts` SET `comment_status` = 'closed', ping_status = 'closed' WHERE `post_type` = 'attachment' AND `comment_status` = 'open';
Last but not least, you can make sure of WordPress plugins to do the job for you, thought you have to be careful with the settings as they can do much more that simply close the comments on attachment page. Some plugins include Disable Comments and Comment Control. It’s possible to uninstall the plugins after the job is done after the change is made in database, and not depending on plugin for it to work.
You can also use a WordPress hook to enforce disabling of attachment (media) comments in database when uploading.