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

How to Disable Image Attachment Pages in WordPress

May 5, 2018
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.

disable-image-page
Article by AJ Clarke WPExplorer Staff
Published on: March 14, 2011
Last updated on: May 5, 2018
Subscribe to the Newsletter

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

29 Comments

  1. Mike says:

    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 says:

      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 says:

      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.

  2. Mike says:

    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.

  3. Jason says:

    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

  4. Jason says:

    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 says:

      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 says:

    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 says:

    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 says:

      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 says:

    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 says:

      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 says:

    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 says:

    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

  10. Sahil says:

    Thanks for sharing this Tecca…

  11. Gene says:

    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 says:

      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 says:

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

  13. Xaibi says:

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

  14. Andreas Diehl says:

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

    • AJ Clarke says:

      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 says:

    What do I do to remove the index Attachment URLs?
    Thanks!

    • Kyla says:

      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.

Leave a Reply

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