Completely remove "attachment" pages for WordPress media. Improve SEO and prevent conflicts between page and image permalinks.
Completely disable “attachment” pages created by WordPress.
By default, WordPress creates a page for each of your attachments. This is can be undesirable because of two reasons:
Attachment pages don’t have any content, except an image, so they provide little value and can negatively affect your SEO because they are so-called thin content. Even worse, attachment pages may in some cases rank higher than your actual content pages which leads to a poor user experience.
Attachment pages can accidentally reserve slugs on your site. Let’s say you upload an image named contact.jpeg, an attachment page https://example.com/contact
is automatically created. If you then try to create a page named Contact, the URL for that page will be https://example.com/contact-2
which isn’t that great.
This plugin works by automatically setting all attachment slugs to an unique id, so they won’t conflict with your pages. If an attachment page is accessed, the plugin will set a 404 status code and display the “page not found” template.
You can also mangle any existing attachment slugs so they won’t cause any issues in the future.
The plugin supports WP CLI.
wp disable-media-pages mangle
wp disable-media-pages restore
WordPress 6.4 includes a new feature that allows you to disable attachment pages. However, this feature redirects attachment pages to the file URL instead of returning a 404 error. To completely disable attachment pages, you should use this plugin instead. The WP 6.4 feature also does not fix the issue where attachment pages reserve slugs for pages.
Also, there is not user interface to enable or disable media pages, they are automatically disabled for new sites but remain enabled for existing sites.
Because of these issues, I recommend you to use this plugin instead of the built-in feature. The plugin will be updated in the foreseeable future, at least until attachment pages are completely removed from WordPress core and older WordPress versions are no longer in use.
Special thanks to Greg Schoppe for his research and inspiration that helped a lot with developing this plugin.
Maintaining a WordPress plugin is a lot of work. If you like the plugin, please consider rating it on WordPress.org. You can also support me on GitHub sponsors. Thank you!
If you are interested, you can also check out my other WordPress plugins:
Go to the Settings ▸ Disable Media Pages ▸ Mangle existing slugs. This will show you a wizard to mangle existing attachment slugs.
First of all, not everyone uses Yoast SEO. More importantly, while Yoast SEO can fix the duplicate content issue, it does not help with issue of media files reserving slugs for pages.
Instead of displaying a 404 HTTP error, some people recommend you to redirect attachment pages to the parent page instead. I think this can be a good short-term solution if the attachment pages have been indexed by Google and you want to preserve SEO ranking for these URLs. There’s a plenty of plugins on the plugin directory that let you to do that. In my opinion returning the 404 error is the correct long-term solution and if you are launching a new site, it’s best to simply disable these pages so they won’t ever end up in Google index.
The unique id is an UUIDv4, without dashes.
Yes, this functionality is available in version 1.1.0. The attachment slug restoration tool allows you to restore the attachment slugs back to ones based on the attachment title.
Yes, check out the GitHub repository.
Note for WordPress 6.4
WordPress 6.4 includes a new feature that allows you to disable attachment pages. However, this feature redirects attachment pages to the file URL instead of returning a 404 error. To completely disable attachment pages, you should use this plugin instead. The WP 6.4 feature also does not fix the issue where attachment pages reserve slugs for pages.
Also, it seems like this feature does not work as intended, because it will disable attachment pages only for users who are logged in. Anonymous users will still be able to access attachment pages. You can follow the progress of this issue on WordPress Trac.
Also, there is no user interface to enable or disable media pages, they are automatically disabled for new sites but remain enabled for existing sites.
Because of these issues, I recommend you to use this plugin instead of the built-in feature. The plugin will be updated in the foreseeable future, at least until attachment pages are completely removed from WordPress core and older WordPress versions are no longer in use.
redirect_canonical
action. Because the plugin didn’t return a value from this filter, this caused the plugin to change default WordPress behaviour (eg. https://example.com/index.php did not redirect to https://example.com/ like with a normal WordPress installation). In this version the filter returns the value which restores this WordPress default functionality. I’m making this a major release because it changes the plugin behaviour, so I recommend testing your site in a development or staging environment before updating your production site. For more information, see this support thread.