Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More

How to Disable WordPress Attachment Pages

Last modified: September 25, 2023

By default WordPress creates a frontend page for all the images and other media uploaded to your site. For some websites it may be nice to have these pages but for most they are simply considered thin content and can negatively impact your WordPress SEO efforts. This tutorial will show you how to remove these attachment pages from your site.

What are the WordPress Attachment Pages?

In WordPress; images, videos, pdf’s and any other media you’ve uploaded to your site that exists within the media library are actually saved as a post type named “attachment”. By default the attachment post type is also public, which means they have frontend posts that are accessible on your site.

You may not have known this because most themes don’t display attachments on the frontend, but if you go to your media library you can easily view any of the posts by clicking the “view” link like such:

Clicking the view link will take you to the live version of your site which should then display a post with the image. The way these pages look depends entirely on your WordPress theme. For example in the Total theme the attachment pages display the media (image/video/pdf/etc) with the media caption and some details about the file.

Why You Should Remove the Attachment Pages

The issue with WordPress attachment pages is that they are virtually pointless and generally have little to no content. Most websites also have more images added to their site then they do posts or pages which makes things even worse. Let’s say you have a website with 500 well written high quality articles and each article has at least 4 images, this means your site will have around 2,000 images. If all of these images also have a post associated with them only 20% of your website will actually be quality content.

When it comes to SEO having high quality content is very important and having low quality content can really bring down your overall ranking. The Fix? Well, simply remove the WordPress attachment pages so that they aren’t negatively impacting your site.

Below I will show you 3 different methods you can use to disable or remove the attachment/image pages in WordPress so you can choose the one that best suits you.

Option 1: Redirect Attachment Pages with the Yoast SEO Plugin

The first option is using the Yoast SEO plugin and the reason for that is because most WordPress site are already using this plugin. All you need to do is install and activate the plugin because by default Yoast “disables” the attachment pages by redirecting them to the actual image or media itself.

While installing Yoast is enough, it’s always a good idea to double check to make sure the Media Pages haven’t been accidentally enabled. To do this you’ll want to go to the advanced settings within Yoast and click on the media pages tab and make sure the option that reads “Enable media pages” is disabled like the screenshot below:

Option 2: Redirect Attachments without a Plugin using a Custom Function

If you aren’t using the Yoast SEO plugin you can easily redirect your attachment pages with a little custom code. Below I’m sharing 2 methods so you can choose the one that you believe is best. I don’t believe it makes a difference in terms of SEO which method you choose, let me know in the comments if you have any insight on the subject.

Important: Don’t insert both snippets into your site because it will cause an error, you will want to only use the snippet you think is best for you.

Method 1: Redirect Attachment Pages to the Parent Page

The following snippet can be used to redirect your attachment pages either to the parent post (aka the post that you were editing when you uploaded the media file) or to the homepage if they were uploaded directly to the media library.

// Redirect the attachment pages to the file itself for SEO reasons.
function wpexplorer_redirect_attachment_page() {
    if ( is_attachment() ) {
        global $post;
        if ( is_a( $post, 'WP_Post' ) && ! empty( $post->post_parent ) ) {
            $redirect = esc_url( get_permalink( $post->post_parent ) );
        } else {
            $redirect = esc_url( home_url( '/' ) );
        if ( wp_safe_redirect( $redirect, 301 ) ) {
add_action( 'template_redirect', 'wpexplorer_redirect_attachment_page' );

This method is nice because it will redirect users to an actual post if your image was uploaded while writing an article and perhaps the user will be interested to stick around and read it.

Method 2: Redirect Attachment Pages to the File

The second snippet can be used to redirect your attachment pages to the file itself. So rather then displaying a post with an image it will redirect to the image itself.

// Redirect the attachment pages to the file itself for SEO reasons.
function wpexplorer_redirect_attachment_page() {
    if ( is_attachment() ) {
        global $post;
        if ( is_a( $post, 'WP_Post' ) ) {
            $redirect = esc_url( wp_get_attachment_url( $post->ID ) );
            if ( $redirect && wp_safe_redirect( $redirect, 301 ) ) {
add_action( 'template_redirect', 'wpexplorer_redirect_attachment_page' );

This method is nice because it will redirect to the file itself which may be good for getting your images indexed in the Google search image directory. But this isn’t really needed since your images will generally be used on your live site within your posts where Google can find them.

Option 3: Redirect via the attachment.php File

Another option is to redirect your attachment pages by creating an attachment.php file in your child theme (or parent theme if working with a custom theme for your own site or client) and then pasting the following code inside the file:

defined( 'ABSPATH' ) || exit;

global $post;
if ( $post && $post->post_parent ) {
    wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
} else {
    wp_redirect( esc_url( home_url( '/' ) ), 301 );

This snippet is essentially the same code we used in option 2 but outside of a function and placed directly into a template file. And for this code we are redirecting the user to either the parent page or the homepage if there is no parent page. If you prefer to redirect the user directly to the attachment file then you would want to use the following code instead:

defined( 'ABSPATH' ) || exit;

global $post;

if ( is_a( $post, 'WP_Post' ) ) {
    $redirect = esc_url( wp_get_attachment_url( $post->ID ) );
    if ( $redirect && wp_safe_redirect( $redirect, 301 ) ) {

When using this method I mentioned you’ll want to create an attachment.php file which will redirect all attachment types (images, videos, pdfs, audio…etc). If you wanted to only redirect images then you can rename the attachment.php file to image.php.

Option 4: Modify Attachment Arguments (Unfortunately this Doesn’t Work)

In an ideal world we wouldn’t have to redirect the attachment pages but rather we could just disable them completely by hooking into the register_{$post_type}_post_type_args WordPress filter and setting the publicly_queryable parameter to false as you would do with any custom post type.


add_filter( 'register_attachment_post_type_args', function( $args ) {
    $args['publicly_queryable'] = false;
    return $args;
}, 100 );

Unfortunately this doesn’t work which to me that seems like an inconsistency in WordPress. I wanted to mention this, because I would have tried this code myself and I didn’t want you to waste time and energy trying to figure out your code wasn’t working.

It’s always better to disable a page then to create a redirection for it because a redirection will basically cause your site to”load” twice. If you are working on an old site that already has a lot of indexed attachment pages you will want to redirect them for SEO reasons but for a new site it would best to disable the attachment pages completely from the beginning.

Hopefully in the future WordPress will either allow this filter to function properly or include a built-in setting for completely disabling attachment pages.


  1. Mike

    Hey mate – thanks for this post.

    I tried this and 2 things happened:
    1) the image urls redirected to the parent post – woohoo!
    2) The attachment pages – now have an infinite loop. why is this happening?

    (there was no image.php file originally in my theme)


    • AJ Clarke | WPExplorer

      Hey Mike,

      The attachment pages should be using image.php as explained in the post and re-directing. So not sure what you mean.

    • m1ndmaker

      Hello Mike,

      that happens if you call an attachment page without an actual parent. It gets stuck in an redirection loop because get_permalink($post->post_parent) returns the attachment page itself.

      • AJ Clarke

        Good tip, thanks for sharing! I’ll update my post (your code didn’t paste).

  2. Mike

    Thanks! One note: I’d make this a 301 redirect.

    This makes it permanent.

    Would be even better if there was a way to make them noindex.

    • AJ Clarke | WPExplorer

      You don’t need no-index if it’s redirecting 😉

  3. Jason

    Thanks for the info. I tried this per another site I saw and put it into the themes main directory. I do not see that you mention a specific directory.

    Where does it go?

    • AJ Clarke | WPExplorer

      Image.php goes in your active themes main folder.

  4. Jason

    That is what I did. What is the best way to see if it actually worked? my site map still shows a ton of pages when submitted.

    Again, I do appreciate the help!

    • AJ Clarke | WPExplorer

      Has nothing to do with the sitemap. To see if its working go to your media library and try and “view” an image. It should redirect home. That’s what this snippet does. To exclude images from your sitemap, this should be an option in your sitemap plugin. I use WordPress SEO by Yoast for my sitemap and it excludes your media by default.

  5. Jason

    I did it and it seems to be working. I miss understood the sitemap part of it as I was being told that google was indexing those image pages.


  6. Haiko

    The redirection works for somehow, but Google-Webmaster-Tools says, that there is an endless loop caused which can’t be finished. Any idea?

    • AJ Clarke | WPExplorer

      I don’t know. Maybe just use a plugin instead if you are having issues. In fact the WordPress SEO plugin by Yoast which I recommend for all SEO needs has a built-in setting for re-directing image attachment pages to their parent page, which is what I use and is better then the custom code here 😉

  7. Idel' Gabdulhakov

    Hello there, AJ!

    Thank you for the post. I have the question — do you know plugin, which can delete all media pages of the attachments? I tryed to search, but no results.

    • AJ Clarke | WPExplorer

      It’s not possible. All media attachments are technically posts so the only way to delete the “attachment page” is to delete the image. The best is to redirect as noted on this post. If using a plugin such as WordPress SEO by Yoast there is also a built-in setting to do this 😉

  8. Tecca

    Definitely go for Yoast’s plugin if you want to do this — but if not, I use the below code and it redirects your attachments with a 301 redirect back to the post they belong to. No infinite loop.

    [edited – PHP disabled in comments for security]

    (Hopefully the above posts correctly)

  9. Tecca

    Er, it didn’t work. If you could edit that post before allowing it through moderation, AJ, that would be great. This is the code I was posting:

    • AJ Clarke | WPExplorer

      Thanks for sharing this.

  10. Sahil

    Thanks for sharing this Tecca…

  11. Gene

    Not sure if this site is still active but the code as shown by post author is a huge loop and needs to be updated. Ppl who are looking for a working solution should use the link as provided by Tecca.

    • AJ Clarke

      Hi. Yes we’ve updated the code and I am actually going to add a new snippet that is better and won’t require editing the image.php file but rather using a simple function in your functions.php file (best when using a child theme).

  12. Oliver Gehrmann

    Isn’t this code better (it makes sure I don’t get the infinite loop when trying to access an attachment page directly)?

    • AJ Clarke

      Yes Oliver, it is better. I have updated the code on the page – thanks!

  13. Xaibi

    What if we want to redirect Page attachment to the same page not Post attachment.

    • AJ Clarke

      The first snippet under “Redirect Via Custom Function” should do exactly that via the post_parent check.

  14. Andreas Diehl

    As I understand it this setting in Yoast is for images attached to posts but not to images attached to pages?

    • AJ Clarke

      Neither. In WordPress whenever you upload an image it is added to a media library. Technically images are considered “posts” so there are pages created for every image you add to your site which is not good for SEO so you can disable these auto image posts.

  15. A gia re

    What do I do to remove the index Attachment URLs?

    • Kyla

      By default all images/media you upload to WordPress are posts so you just need to use a plugin to disable media attachment pages. For example, if you’re already using the Yoast SEO plugin there’s an option (under advanced > permalinks) to redirect attachment pages to the parent post URL.

  16. Andy

    Maybe I’m not reading correctly but wouldn’t you want optimized images (file name, tags, titles, etc) included as part of your overall SEO by having Google index images in addition to pages/posts?

    Or is removing pages with empty content better than the SEO boost from indexed images? This is a sincere question.

    Also, can all images just be detached and then use Yoast to redirect?

    • AJ Clarke

      Hi Andy,

      Google and other search engines will still index your images themselves, what this does is remove the auto generated pages that WordPress creates for each image which have no content and for most sites serve no purpose. You aren’t disabling access to the image itself just the WP generated posts. Make sense? Even if you “detach” the images WordPress still create a post for them which can be accessed on the front-end which is why you need to redirect all of them either via code or Yoast’s redirection setting.

  17. Jelena

    The code for functions.php works like a charm! 🙂
    Thanks so much for sharing this, guys!!

  18. morisfa

    thanks this post is the best

    other sites just give a help for yoast seo but you shared a Function code \ thats working

    good job

  19. Khaled

    Thank you for the post 😉

  20. Nenad Dragojevic

    Why are you making misleading titles? “How to Disable Image Attachment Pages in WordPress” and you talk only about redirecting them. There’s no solution how to disable them. Please use your titles properly don’t waste people’s time with misleading articles. Jesus…

    • AJ Clarke

      Hi Nenad,

      It’s not possible to completely remove the singular page because the attachment post type is registered by core and if you try and hook into the register_post_type_args filter to set the “publicly_queryable” arg to false it won’t do anything. So the only good alternative is to redirect the attachment pages. Sure you could hook into the rewrite_rules_array filer, but this will only work if you are using the “plain” permalink structure (which you should never use if you care about SEO) and can also cause some issues with 3rd party plugins.

      I’m not sure what your concern is with redirecting the posts. You most likely aren’t linking to your attachment pages anywhere so it doesn’t matter. The purpose of the redirection (disabling the attachment page) is simply if someone accidentally comes across the post. Not even search engines like Google will find these attachment posts unless you link to them from somewhere or include them in your sitemap for some reason.

      So redirection is the way to go and we try and publish the best solutions here at WPExplorer!

      • Xavier O'Neill

        Thanks for the post (most complete I’ve come across) and this very valid explanation here.

  21. John Woo

    It’s working! cheers on the redirection instruction!

Leave a Reply

Your email address will not be published. Required fields are marked *

Learn how your comment data is processed by viewing our privacy policy here.