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.
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)); ?>
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.