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

How to Disable Image Attachment Pages in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
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 experts 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 »


  1. Sam says:

    Thank you dude

  2. Ben says:

    Or You can write this line on robots.txt

    Disallow: /wp-content/uploads/

  3. Ajit says:

    Thank you, yes I done, very helpful topics, thank you again

  4. Laurence Cope says:

    But how do you actually disable attachment pages? This post doesn’t disable them but puts a redirect in place. We have issues where the redirects is conflicting with actual page names. We dont want any attachment pages and therefore no need for redirects.

  5. paul says:

    I would like to keep the attachment pages but within the attachment pages that display the image in a bigger size to disable links to prev next and full size image as this is where they can copy images or see images that I dont want non registered users to see etc. How can we do that?

  6. Whitney says:

    Oh wait, I just realized something… if I no index the media files, does that mean all my media is not indexed or just the attachment pages?

  7. Whitney says:

    Is the duplicate content issue only solved if I “no index” the pages or will the 301 redirect to parent take care of that too? I’m assuming I’d need to no index them…. but just want to make sure I’m assuming right :)

  8. Muhammad Furqan says:

    Hello i also disabled attachments pages in sitemap. will this technique prevents google to index images?

    • WPBeginner Support says:

      If the images appear in posts as well, then no it will not stop Google from indexing images.

  9. Sophia Martin says:

    Thanks for the quick help, it fixed things at once!

    Thanks again!

  10. Devdiz says:

    Thanks alot

  11. marco says:

    Great solution!
    May I add that after the modification on Yoast I had to go to Settings->Permalinks and save settings (no need to change anything).
    Your mileage may vary, this worked for me, thanks!

  12. Stef says:

    It seems Yoast has changed things and this no longer works. Or something is stuck in cache.

    • Jodi says:

      I’m having the same issue and I don’t think it’s the cache. I installed the plugin mentioned and that works fine.

  13. Hicham says:


    Please how to create image.php ?

    Thank you

  14. Henry says:

    A nice informative article but oh my what a misleading title!
    The post title and the last sentence (after the comma) of the first paragraph are totally off!!!

    This is NOT disabling attachment pages/nor stopping WP from creating them (as the title implies) but redirecting links to it to somewhere else, and that is something real different!

    Disabling is stopping some from to be happen, and whatever code is shown in this article (even used by named plugins) it is not stopping WP from creating these attachment pages. This article is about how to redirect , which is okay ofcourse but not as implied by the post title and article as it. Such a shame because that would be a scoop!

  15. Angela says:

    Hi, thanks for the info!

    What about older images which were previously indexed by Google?

    I have had the setting on in Yoast for “redirect attachments” for at least 6 months now, but my older images are still being found.

    What would anyone recommend to do with these images? It concerns me because I get some critical errors on the ahrefs tool:

    duplicate title tags
    duplicate meta descriptions

    Shall I just go and add all the tags?

  16. Simon G.Proudfoot says:

    Putting the line in my image.php file worked perfectly. It was giving me a bad SEO score beforehand as the links to the images weren’t mobile friendly. This has fixed it. Thanks :)

  17. Priyanka Biswas says:

    Glad I found this article. Was wondering why Google was showing all my image attachment pages in the search results. This helps! Thank you.

  18. Raleigh Leslie says:

    Thanks this post helped me explain to someone why you would want to redirect media attachment pages to the actual post of page containing to media. re: “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.”


  19. sam says:

    mine is already checked but doesn’t work, all my images redirected to an other page :(

  20. Tim Brugman says:

    Thanks for the code. I made a slight adjustment so pages for media uploaded to the library directly, not a post, will redirect the visitor to the homepage. Feel free to use this:

  21. Catherine says:

    Thank you so much for helping me solve the problem of image attachments. I inserted the code

    post_parent)); ?>
    into my image.php file and it worked like a charm.
    So will Google now de-index my attachment page url’s?

  22. Chris Cox says:

    Ok. Using the php code (no plugin) what happens if there are two pages/posts using the same image?

  23. ashar says:

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

  24. Tonya says:

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

  25. Jorge Pinto says:

    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.

  26. philip says:

    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

    • WPBeginner Support says:

      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.

  27. Jess says:

    Is it ok to delete the attachment posts in phpMyAdmin?

  28. karen says:

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

  29. Sam says:

    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.

  30. Elektra says:

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

  31. Mark Law says:

    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.

  32. Junaid Abbas says:

    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.

  33. Mark says:

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

  34. Shannon says:

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

  35. Bruce Simmons says:

    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.

  36. Sharon Rigano says:

    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.

  37. yogesh says:

    Thanks! This is a very useful tutorial.

  38. Ahmad Raza says:

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

      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 '’;
      } ?>

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.