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

How to Disable Image Attachment Pages in WordPress

Last updated on:
Why Images Are Important

The other day i was going through and doing some on-page SEO clean-up of my site and I noticed multiple “image pages” indexed on Google. Whenever you add an image to your post it automatically attaches a link to the image that goes to a blank post (WordPress stores attached image data as posts in the db) with nothing but that image and maybe a title and caption (depending on the theme you use).

The issue is that these pages are virtually pointless as they have no content. You are better off linking to your actual image itself if you want people to be able to click and see a large version or using a lightbox plugin for your posts. The attachment pages in WordPress are what’s considered “thin” content, so they hold no value and can actually harm your SEO efforts.

In this article I’ll show you different ways you can disable this main image attachment page on your WordPress site and redirect your page rank back to it’s primary article or your site homepage. And redirecting these attachment posts via 301 redirects is the best solution for both usability and SEO.

Redirect Image Attachment Pages With The Yoast SEO Plugin

The easier way is to redirect your attachment pages by using a plugin, and a free one at that. Here are the 3 easy steps:

  1. Install and setup Yoast SEO for WordPress
  2. Once setup, click on SEO > Advanced and click on the Permalinks tab
  3. Enable the option to “redirect attachment URLs to parent post URL” and save

Redirect Via Custom Function

You can also create your own custom function for redirecting your attachment pages to either the homepage or the post parent. This function should be placed in your functions.php file (preferably in a child theme if you are using a theme by another developer).

function myprefix_redirect_attachment_page() {
	if ( is_attachment() ) {
		global $post;
		if ( $post && $post->post_parent ) {
			wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
			exit;
		} else {
			wp_redirect( esc_url( home_url( '/' ) ), 301 );
			exit;
		}
	}
}
add_action( 'template_redirect', 'myprefix_redirect_attachment_page' );

If you notice the code will only redirect when is_attachment() returns true. This means it will redirect for any attachment type not just images, which is usually best. However, you can target only image attachments by adding some extra checks to see what the current attachment type is.

Redirect Via The image.php File

Your second option is to completely disable your attachment pages by adding a WordPress redirect directly added to the top of your image.php file.

  1. Create a new file called image.php in your child theme (always best to work with child theme’s when editing a theme) or if you are developing your own theme create an empty image.php file in your theme.
  2. Insert the code below in your image.php file
<?php
global $post;
if ( $post && $post->post_parent ) {
	wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
	exit;
} else {
	wp_redirect( esc_url( home_url( '/' ) ), 301 );
	exit;
}

Now when you refresh your image attachment page it should redirect to the original post where the image was uploaded or if it was uploaded directly in the media library it will redirect to your site’s homepage.

Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

33 Comments

  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)

    Thanks
    Mike

    • 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?
    Thanks!
    Jason

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

    Thanks!

  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:

    http://wordpress.org/support/topic/why-plugin?replies=3

    • 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 Avatar 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 Avatar 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 Avatar 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 Avatar 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?
    Thanks!

    • Kyla Avatar 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 Avatar 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

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.