Media PHP Beginner

Disable WordPress Attachment Pages

Last updated: May 6, 2026

Every image, PDF, and file you upload to WordPress gets its own attachment page, a publicly accessible URL that displays the file alongside a minimal template containing almost no content. These pages serve no practical purpose for visitors and are a persistent source of SEO problems. They’re thin content pages that Google may index and penalise, they dilute your crawl budget by adding hundreds or thousands of URLs that offer nothing of value, and they often rank for image-related search terms instead of the actual post the image belongs to.

The cleanest fix is to redirect all attachment page requests to their parent post, or to the homepage when the attachment has no parent, for example, images uploaded directly to the Media Library without being attached to a post.

The Code

Add this to your functions.php or a site-specific plugin. It hooks into template_redirect, which fires after WordPress has determined what page is being requested but before any template is rendered. If the request is for an attachment page, it issues a 301 permanent redirect and exits.

Why 301 and Not 302

A 301 redirect tells search engines that the move is permanent and that link equity and crawl signals should be transferred to the destination URL. A 302 is temporary and doesn’t transfer those signals. Since attachment pages should never be accessible, 301 is the correct status code here. Once Google processes the 301s, it will stop crawling the old attachment URLs and consolidate any ranking signals they had accumulated to the parent post or homepage.

Parent vs No Parent

WordPress tracks which post an attachment was uploaded from as the attachment’s parent. When you upload an image while editing a post, that post becomes the attachment’s parent. When you upload directly to the Media Library without having a post open, the attachment has no parent, post_parent is 0.

The snippet handles both cases: attachments with a parent redirect to that parent post’s permalink, and orphaned attachments redirect to the homepage. You can change the orphan destination to any URL, a dedicated media page, a portfolio archive, or a 404, depending on what makes most sense for your site.

Existing Indexed Attachment Pages

If your site has been live for a while, Google may have already indexed attachment pages. After adding this snippet, submit your sitemap to Google Search Console and use the URL Inspection tool to request recrawling of a sample of the old attachment URLs. Google will follow the 301s, drop the attachment pages from its index, and transfer any accumulated signals to the parent posts over the following weeks.

Compatibility with SEO Plugins

Some SEO plugins, including Yoast SEO, have their own setting to redirect attachment pages. If you have Yoast’s attachment redirect enabled and also add this snippet, two redirects will compete. Check your SEO plugin settings before adding this snippet, and disable the plugin’s attachment redirect if you prefer to manage it in code.

functions.php
add_action( 'template_redirect', function() {
    if ( ! is_attachment() ) return;

    $parent_id = get_post()->post_parent;

    if ( $parent_id ) {
        wp_redirect( get_permalink( $parent_id ), 301 );
    } else {
        wp_redirect( home_url( '/' ), 301 );
    }

    exit;
} );

Built by Nahnu Plugins

Need something more powerful than a snippet?

Our commercial plugins go further, built for serious WordPress sites with full support, updates, and documentation included.

Browse All Plugins →

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only