Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Disable Image Attachment Pages in WordPress

Last updated on by
Elegant Themes
How to Disable Image Attachment Pages in WordPress

WordPress by default creates a single page for each attachment that you have on your site. This includes images, audio/video files, pdfs, etc. This functionality is great for photographers, but not so much for the rest of us. Often we get complaints from beginners who accidentally linked their images to the attachment pages, and they don’t like the way it looks. That’s a big issue because a lot of themes don’t have special templates for the image attachment pages. Sometimes an image on your website can become popular and people might start landing on the attachment page directly from Google. Ideally you would want them to land on your post and see the image in the context you have used it. In this article, we will show you how to disable image attachment pages in WordPress.

If you are already using WordPress SEO by Yoast plugin, then go to your SEO » Permalinks options page. All you have to do is check the box next to “Redirect attachment URL’s to parent post URL”. This will redirect users arriving at attachment pages to the parent post.

Disable attachment pages and redirect users to parent post

For those who are not using the WordPress SEO by Yoast plugin, you can still disable image attachment pages and redirect users to parent post using another plugin called Attachment Pages Redirect. All you have to do is install and activate the plugin. Upon activation the plugin starts redirecting users landing on attachment pages to parent post. If no parent post is found, then the users are redirected to homepage. This plugin works out of the box and has no settings page. Simple and easy.

If you are among those who don’t like plugins, then there is an option for you as well that. Create a new file in your WordPress theme folder and name it image.php. If your theme already has an image.php file, then you need to edit that instead. All you have to do is add this code as the first line in your image.php file :

<?php wp_redirect(get_permalink($post->post_parent)); ?>

Save image.php file and upload it into your theme directory. Now when a users reaches the image attachment page, then they will be redirected to the parent post.

Lastly, just want to emphasize that there is nothing wrong with using good plugins. See our article on how many plugins should you install on your site. We recommend that everyone use WordPress SEO by Yoast plugin, so doing this is as simple clicking on one checkbox.

On our site, we haven’t disabled image attachments entirely because there are some instances where we want to send folks to a specific chart or image that we have on the site. Instead what we have done is that we excluded it from our sitemap. Furthermore, we have added a meta robots text noindex, nofollow to all of our attachment single pages using Yoast’s SEO plugin. You can do this too by going to Titles & Metas tab and going under Post Types. Simply check the noindex, nofollow tag for ONLY the attachment post type.

We hope that this article helped you disable image attachment pages in WordPress and redirect users to parent posts. If you have any questions or feedback, then please let us know by leaving a comment below.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • ashar

    I just checked the redirect in SEO as these things were driving me nuts. so glad I found you. thanks – best ashar

  • Tonya

    Thank you! This was an excellent tip, just what I needed to help clean up errors found by Google.

  • Jorge Pinto

    I tryed the code you suggest, it works with the images associated with posts os pages, but.. all the other images uploaded that are no assigned to any post, for that images your code does not work.

  • philip

    Hello a user called Sam posted this issue – You write “Simply check the noindex, nofollow tag for ONLY the attachment post type.” but I do not see such an option. Are you referring to the “media” section? If I click no index under media will that no index my images or attachments? I would like to no index my attachments but I don’t want to lose traffic from Google images.

    I would also like to know the answer as I have the same issue
    thanks
    philip

    • http://www.wpbeginner.com/ WPBeginner Support

      Philip, no this should not affect your Google Image Search traffic as the option redirects users landing on attachment page to the parent post URL where your image is available for search engines to index.

  • Jess

    Is it ok to delete the attachment posts in phpMyAdmin?

    • http://www.wpbeginner.com/ WPBeginner Support

      No, you are not supposed to delete data directly from your database.

  • karen

    I tried adding the code you have given to my image.php file and it worked beautifully. thank you!

  • Sam

    You write “Simply check the noindex, nofollow tag for ONLY the attachment post type.” but I do not see such an option. Are you referring to the “media” section? If I click no index under media will that no index my images or attachments? I would like to no index my attachments but I don’t want to lose traffic from Google images.

  • Elektra

    thanks so much for posting! very useful info and straight forward! appreciated!!

  • Mark Law

    Thanks for the heads up, I got a bit of a shock when I saw absolutely everything had been indexed, including elements of the template such as image backgrounds and so on. I’m using Yoast so it is an easy fix – I wonder though if it is worth still having pdf attachment pages indexed – I’ll look into it.

    • http://www.wpbeginner.com/ WPBeginner Support

      It takes users directly to the PDF, if that’t what you want then yes sure.

  • Junaid Abbas

    Is this possible to stop attachment pages to be indexed??? I do not want to redirect them to the home page. I just want to stop indexing them.

  • Mark

    I love the Internet, within 2 mins of discovering an issue, I had found this post and resolved the problem! Thank you wpbeginner.com

  • Shannon

    Will this also fix the issue with creating duplicated content where the image holds the same title as the associated post?

    • http://www.wpbeginner.com Editorial Staff

      Yes.

  • Bruce Simmons

    Heads up about the plugin… if you use the gallery option to present images in a post, the plugin blocks the function and clicking on an image brings the user back the thumbs index of the images.
    -Bruce

  • Sharon Rigano

    Thank you for this article. It has come just at the right time as I was trying to work this one out. Turns out it is a simple solution.

  • yogesh

    Thanks! This is a very useful tutorial.

  • Ahmad Raza

    Thanks for the tutorial. I became victim of Google Panda just because of indexing of alot of attachment pages from my photo blog.
    Is there any way to no index only attachment pages (not images) from Google?
    Because i have a photo blog and i don’t want to redirect my photos to main posts.

    • Alex

      Ahmad Raza, here’s soulution for you

      1 || is_author() || is_tag() || is_date() || is_attachment()){
      echo ”;
      } ?>

      You can add just this:

      <?php if( is_attachment()){
      echo '’;
      } ?>