In a project I’m working on, I made an interesting discovery this week. The website had only two comments – it’s a WooCommerce shop, so comments are not really important. Those two comment however were made on an attachment page. The page had the title “contact-2” and was a header image. Most probably, someone searched for the “contact” page of the website and found this page with the comment form, thinking that this is the contact form.
What is an attachment page?
For those of you, who never came across an attachment page, let me quickly explain, what they are. Some of you might know, that every item you upload into the media library, a database entry is created. Those entries are in the wp_posts
table with the post type attachment
. So every media item it “a post”. And since the attachment post type is set to public
, every media item has an attachment page.
In the case mentioned above, there was a file with the name “contact.jpg” uploaded. Since there was already a regular page with the title “contact”, WordPress appended the “-2” suffix. I have a blog post about this suffix and how to remove it. But what does that mean? If you access a URL like https://example.com/contact-2, you would get a page, that may be using the singular.php
template of your theme, unless the theme has a better template for attachments.
On this page, you would see the media item, in the case of an image, you would see the full size. You can see an attachment page on the demo site for the Twenty Nineteen theme. This URL is using the attachment_id
parameter, using the post ID of the attachment (media item). You could also use the p
parameter, which then redirects. But depending on your permalink settings, you might also see a “nice URL” like the “contact-2” mentioned earlier.
Why you (don’t) want to have an attachment page
If you have a media heavy blog, and you want to allow commenting your media item, the attachment page might make sense. But for most pages, they would just “blow up” the pages of your website, and they can be in conflict with some “normal pages”.
Those attachment pages are also visible to search engines, and since they only use the file name as the title, you might end up with a page getting (more) traffic to those pages, and not to the normal pages you want to have traffic to. Fortunately, the attachment pages are not showing up in the sitemaps.
How to remove the attachment pages?
You can’t really “remove” them. You can make them “not public”, but there is a simpler way. If you set up a new WordPress site starting with WordPress 6.4, you can stop reading this blog post, since you already have a solution.
If your WordPress installation is older, you can use a new option that was introduced in WordPress 6.4, to redirect an attachment page URL to the media file URL.
Set the option with the WP-CLI
I usually use the WP-CLI for such tasks. If you also use it, you can run the following command:
wp option set wp_attachment_pages_enabled 0
A value of 0
for the wp_attachment_pages_enabled
option disables the attachment pages, and the redirect_canonical()
function would then redirect to the media file.
Using the option.php page
If you don’t use the WP-CLI or you can’t use it, you can also use a “hidden feature” in the WordPress Dashboard. Navigate to wp-admin/options.php
, where you can view and edit options. Here you search for the option name, change the value from 1
to 0
and save the options “Save Changes” button at the end of the options page.
Using a SEO plugin
Some SEO also have an option to redirect the attachment pages (or deactivate them differently). I use Yoast SEO on most of my pages, and here you would find it as “Yoast SEO > Settings > Advanced > Media pages”. In this section, you toggle the “Enable media pages” off. If you instead want to use the pages, you can set some other settings here for attachment pages.
Conclusion
It’s easy to forget that attachment pages exist in WordPress. There were probably good reasons to have them in the past, but on a typical website, they are often not something you want to have traffic to. Fortunately, you can “disable” them without additional plugins now.