This snippet disables access to individual media attachment pages by returning a 404 response. It is useful for websites that do not need separate attachment pages for uploaded images and media files.
Steps:
-
Log in to your WordPress dashboard.
-
Navigate to Appearance → Theme File Editor.
-
Open your active theme’s
functions.phpfile. -
Add the PHP snippet at the bottom.
-
Save the file.
-
Open an individual media attachment URL on your website.
-
Verify that the page displays a 404 response.
Note: This disables attachment pages, not comments on all media-related content.
function wpf_disable_attachment_comments() {
if ( is_attachment() ) {
wp_die(
esc_html__( 'Media attachment pages are disabled.', 'textdomain' ),
'',
array( 'response' => 404 )
);
}
}
add_action( 'template_redirect', 'wpf_disable_attachment_comments' );
Tested on
Reported by the author on December 1, 2026. WPFolks has not verified this.
Not tested against WordPress 7.1.5. It may still work.
