WordPress by default creates a single page for each attachment you have on your site. This includes images, audio/video files, PDF files, etc. This functionality is great for photographers, but not so great for the rest of us. We often get complaints from beginners who accidentally link their images to attachment pages, and don't like the way they look. That's a big deal because many themes don't have special templates for image attachment pages. Sometimes an image on your website can become popular and people can start coming to the attachment page directly from Google. Ideally, you want them to appear in your post and see the image in the context in which you used it. In this article, we will show you how to disable image attachment pages in WordPress.
If you are already using the WordPress SEO by Yoast plugin, go to its SEO »Permalinks options page. All you have to do is check the box next to “Redirect attachment URLs to the parent post URL” . This will redirect users who land on the pages attached 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 the main post using another plugin called Attach Page Redirection. All you have to do is install and activate the plugin. Upon activation, the plugin starts redirecting users who land on the pages attached to the parent post. If a parent post is not found, users will be redirected to the home page. This plugin works out of the box and does not have a settings page. Simple and easy.
If you are among those who do not like plugins, then there is an option for you as well. Create a new file in your WordPress theme folder and name it imagen.php
. If your theme already has an image.php file then you need to edit it instead. All you have to do is add this code as the first line in your imagen.php
file :
post_parent)); ?>
Save imagen.php
archive it and upload it to your theme directory. Now when a user lands on the image attachment page, they will be redirected to the parent post.
Lastly, I just want to emphasize that there is nothing wrong with using good plugins. See our article on how many plugins you should install on your site. We recommend that everyone use the WordPress SEO by Yoast plugin, so doing this is simply clicking a checkbox.
On our site, we haven't completely disabled image attachments because there are some cases where we want to send people to a specific graphic or image that we have on the site. Instead, what we have done is we have excluded it from our sitemap. Additionally, we have added a meta robots text noindex, nofollow to all of our individual attachment pages using the Yoast SEO plugin. You can also do this in the Titles &Metas tab and Post Types. Just check the noindex, nofollow tag for ONLY the attached message type .
We hope this article helped you to disable image attachment pages in WordPress and redirect users to main posts. If you have any questions or comments, please let us know by leaving a comment below.