When you upload a file such as an image, WordPress creates a URL for that file. This URL is accessible through a search or by direct link. However, some may want to disable the attachment pages for various media types.
In this tutorial, I’ll show you how to make WordPress disable those attachment pages for media. In fact, I’ll show you three separate ways to do this.
What are Attachment URLs?
Attachment URLs act and appear much like any other post or page in WordPress. The biggest difference is the file is the only thing showing. They will use a custom permalink in WordPress much like posts and pages.
This means a visitor can click on a link from Google and see a page displaying just a single image with no other content.
Well, except for your sidebars and footer.
For an unsuspecting user, it could appear the site is incomplete or lacking the information he or she is looking for. In other words, you could lose an opportunity to engage this individual. And you might even make the user subconsciously avoid your website if it happens frequently enough.
How Can I Check if Attachment Pages Exist?
While attachment pages act as a post, they do not actually generate a post that you can view and delete. Thus, it is entirely possible that they currently exist on your website without your knowledge.
Instead, the only way to really find these is to do a Google search on your website.
Go to Google and search for «Sitename: URL» and you will see a list of results. Simply go through the search results and analyze the URL you see. If you see the word «attachments» then you know the page exists.
However, if you have thousands of images and media files, then trying to identify each media file that is causing it is futile. Instead, you will need a bulk action method.
Luckily, all of the methods below are bulk actions.
How to Disable Attachment Pages in WordPress
Method 1: Using Yoast SEO
The first method to disable media attachment URLs is by using Yoast SEO. This plugin is full of tools to manage how your site appears in engines like Google. One of which is how it handles a redirect for media URLs.
If you don’t have it, I suggest installing Yoast SEO. It’s an incredible tool to help you score better in search results as well as creating content for visitors.
For myself, it’s usually one of the first plugins I install on any site I help develop.
Step 1: Go to Media Settings
From your WordPress dashboard, go to SEO and click, «Search Appearance.»
Click the «Media» tab from the top.
Step 2: Redirect URLs
Click the «Yes» button under Media & attachment URLs. This will set media URLs to redirect to the file itself and not create a special page on your website.
This will remove the SEO options in Yoast for file attachment pages. Click the «Save changes» button.
Yoast SEO will automatically redirect all attachment pages. If this is your first time installing Yoast, make sure to take advantage of its other SEO features. It really is the best plugin when it comes to search engine optimization and creating posts.
And the best part is that you can see these tips while you are writing content!
Method 2: Using Attachment Pages Redirect
The Attachment Pages Redirect plugin is a fine tool to use. It will create redirects according to how a user lands on an attachment page.
For instance, the plugin will create a 302 redirect to the home page if the original post is in the trash but users still access the website through the image or file URL. This means there is no 404 file not found error shown to the visitor.
This is by far the easiest method because it works upon activation.
Install and activate the Attachment Pages Redirect plugin.
Once activated, the plugin will automatically begin redirecting users. There are no settings or options to manage in this plugin.
It’s probably one of the easiest tools to make WordPress disable attachment pages for media. You essentially set it and forget it.
Method 3: Adding a Code Snippet
Another way to remove media URLs from WordPress is by adding a code snippet to the image.php file of your theme.
You can do this in a number of ways. Some prefer to use FTP programs such as FileZilla to edit website files. For this tutorial, I’m going to just use the File Manager tool in cPanel.
Of course, it’s always best to make adjustments like this in a child theme. That way if the theme is updated, you don’t run the risk of having the snippet removed.
Note: Since you will be editing code, I strongly suggest creating a backup of your website. This will ensure that if a mistake is made, it can be corrected with minimal effort. That said, this is really just copying and pasting a line of code, so don’t worry too much.
Step 1: Locate Image.php File
Access the wp-content folder of your site from the root directory.
Open the themes folder.
Access your theme’s folder from the list. In this tutorial, I’m using the «twentysixteen» theme. If you’re not sure which you’re using, you can always click the «Appearance» option in WordPress to see which you have activated.
Click on the image.php file and select the Edit option.
Step 2: Add Code
In this case, all you need to do is copy and paste the following line of code:
Note: Make sure that this code is on its own line. If you put it into another function, you will cause an error on your website.
Once you save the changes, WordPress will redirect the attachment URL to the original post. Keep in mind that if you change your theme, you will have to repeat this method.
This is why it’s ideal to use a child theme for custom code snippets and other modifications.
Do Attachment Pages Hurt Rankings?
Sometimes, an attachment page only contains an image, thus it has no text. This can trick Google, and other search engines, into thinking that your website is producing low-quality content. As a result, this can affect the rankings of your entire site.
Another problem with attachment pages is that Google will count them as duplicate content. After all, this is an image from another page or post on your website. And duplicate content can absolutely tank your rankings.
Thus, you must fix this problem before your website begins to rank lower as a result.
Clean Up WordPress and Remove Media URLs
These are only a few methods to set WordPress to disable attachment pages for media. With various plugins, you have access to a myriad of options. In many instances, plugins that focus on SEO support have the option for redirecting attachment URLs.
It’s all a matter of finding the best method which works for you. However, regardless of your method, it is imperative that you disable attachment pages in WordPress. This can hurt your website’s ranking and undermine all of the hard work that goes into them.
How often do you have visitors access attachment URLs without exploring the site further? Do you prefer coding or plugins when it comes to making adjustments such as this?
Author: Michael Brockbank
Michael has been in the tech industry in some form or another since the late 1990s. He’s built a variety of websites using HTML, Joomla and WordPress. Starting his freelance writing career in 2012, he’s completed thousands of projects world-wide covering a wide scope of topics. Michael is currently the Content Marketing Team Lead. Today, he manages a team of writers to create tutorials, blog posts and support content for customers. Outside of GreenGeeks, he manages a YouTube channel that helps others learn how to become freelance writers, bloggers and overall professionals.