How to Disable Image Attachment Pages in WordPress

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.

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.

So after a bit of browsing a found a really good solution to help you “disable” these image pages and bring the page rank back to your main post. Technically it won’t disable the image attachment pages, but it redirects the page back to the primary post.

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.

  • Updated on:
  • Posted Under: Tutorials
AJ Clarke
Post Author: AJ Clarke

Hey. My name is AJ and I am the boss around here. I own and operate WPExplorer. A website dedicated to everything I love about WordPress.

Disclosure
Got something to say? Join the discussion.
  1. 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:
      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.
      Admin
    • 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.
      • AJ Clarke says:
        AJ Clarke
        Good tip, thanks for sharing! I'll update my post (your code didn't paste).
        Admin
  2. 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. 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 says:
      AJ Clarke | WPExplorer
      Image.php goes in your active themes main folder.
      Admin
  4. 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:
      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.
      Admin
  5. 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. 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:
      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 ;)
      Admin
  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:
      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 ;)
      Admin
  8. 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. 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. Thanks for sharing this Tecca...
  11. 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:
      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).
      Admin
  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)?
    • AJ Clarke says:
      AJ Clarke
      Yes Oliver, it is better. I have updated the code on the page - thanks!
      Admin
  13. What if we want to redirect Page attachment to the same page not Post attachment.
    • AJ Clarke says:
      AJ Clarke
      The first snippet under "Redirect Via Custom Function" should do exactly that via the post_parent check.
      Admin
  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:
      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.
      Admin

Leave a Reply

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