The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Disable Comments on Media Attachments

Shared by Amit Dholiya · December 1, 2026 · @template_redirect

1 view
1 upvote
Back to Snippets

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:

  1. Log in to your WordPress dashboard.

  2. Navigate to Appearance → Theme File Editor.

  3. Open your active theme’s functions.php file.

  4. Add the PHP snippet at the bottom.

  5. Save the file.

  6. Open an individual media attachment URL on your website.

  7. 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

WordPress 6.8

Reported by the author on December 1, 2026. WPFolks has not verified this.

Not tested against WordPress 7.1.5. It may still work.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly supported by