joppuyo / disable-media-pages

WordPress plugin to disable "attachment" pages automatically created for WordPress media
https://wordpress.org/plugins/disable-media-pages/
GNU General Public License v2.0
19 stars 2 forks source link

Changes to attachment pages in 6.4 #41

Closed vinkla closed 11 months ago

vinkla commented 11 months ago

As of WordPress 6.4, attachment pages for new WordPress installations are fully disabled.

https://make.wordpress.org/core/2023/10/16/changes-to-attachment-pages/

Is this change rendering this plugin obsolete? For existing sites we can probably just add a filter:

// Disable attachment pages.
add_filter('pre_option_wp_attachment_pages_enabled', '__return_zero');
joppuyo commented 11 months ago

Hey! I'm not sure about this yet because WordPress 6.4 hasn't been released yet.

This change in core should fix the issue with duplicate content on new sites but old sites will still have this issue. Enabling/disabling media pages is done using a hidden configuration flag and it's not exposed in the UI. I might update the plugin to use this flag in WordPress versions greater than 6.4.

It's still not clear if the change fixes the issue where attachment filenames reserve slugs from pages. If this issue still continues even if the attachment pages are disabled, that's one thing this plugin does better than the option in core.

Anyway, I still plan on supporting this plugin in the foreseeable future. I will probably update the readme to direct users to this new filter after 6.4 is released. We'll have to see if the slug issue persist. If it does the plugin might still have some value. If it's fixed in core then 6.4 will probably render this plugin mostly obsolete, at least for new sites.

joppuyo commented 11 months ago

It looks like someone made a copy of this plugin for people who use the Acorn framework: https://github.com/Log1x/acorn-disable-media-pages

At least according to this person the slug issue still persist on 6.4. But I'm gonna wait until the stable release is out. Beta and RC versions are not final and functionality might be changed before the final release.

vinkla commented 11 months ago

I'm not sure about this yet because WordPress 6.4 hasn't been released yet.

WordPress 6.4 was released on November 7, 2023 (yesterday).

It's still not clear if the change fixes the issue where attachment filenames reserve slugs from pages.

I am unsure about new instllation, but I tried to add the filter on an installation that was upgraded from 6.3. Now all attachment page links redirect to the file instead.

joppuyo commented 11 months ago

Yeah my bad, I completely missed 6.4 was actually already released. I did some quick testing and here are my findings:

The issue where media files reserve slugs from pages still exists. For example if I upload an image called example.jpeg, it will reserve the page slug example, even if the attachment pages have been disabled (wp_attachment_pages_enabled is 0). This is so that the redirects still work (https://example.com/example/ redirects to https://example.com/wp-content/uploads/example.jpeg). This is the same thing SEO plugin like Yoast do which I think is insufficient to fix the issue.

What's even weirder is that the redirection behavior only kicks in for users that are logged in. If I have disabled media pages via the core option and I'm logged, when I visit https://example.com/example/, it will redirect me to https://example.com/wp-content/uploads/example.jpeg.

However, if I'm not logged in, basically if I'm an anonymous user, when I visit https://example.com/example/ the attachment page is still displayed (!!!).

Could you check if this happens in your end?

It seems this is because read_post capability is checked before the redirection is performed. Anonymous users don't have any capabilities so the redirection doesn't kick in.

https://github.com/WordPress/WordPress/blob/e2d61d78ede843c2d05f44a4e15f30a57fb47d95/wp-includes/canonical.php#L553C11-L553C11

If this is how this feature is implemented in the core then it's pretty half-baked and barely works as intended.

vinkla commented 11 months ago

I tried it locally and you're correct. I can reproduce your steps. When I'm logged out and visit the /example slug, the page displays the image as if it were a featured image. This update by WordPress was definitely half-baked. I wonder what they thought this would achieve. However, understanding how Automattic updates WordPress is a topic for another day. I'm glad this plugin will continue to exist.

vinkla commented 11 months ago

I'm curious if disabling wp_attachment_pages_enabled still serves a purpose. Will you include it in this plugin?

joppuyo commented 11 months ago

I have released a new version of this plugin that is compatible with WordPress 6.4. Basically only thing this fixes is the following case.

  1. Attachment pages are disabled using the core option
  2. User is logged in
  3. User visit an attachment page

The fix disables redirection in this case and instead displays the 404 page.

Otherwise, the plugin currently has following advantages over the core feature:

  1. Attachment pages are "disabled" for anonymous users and logged-in users. The core feature only "disables" attachment pages for logged-in users
  2. Attachment pages display 404 template instead of a redirection
  3. File names no longer reserve page slugs

Core devs will probably fix the first issue on a patch version but even after this, points two and three still stand. I have written a comment about the logged-in problem in the WordPress issue tracker.

joppuyo commented 11 months ago

I'm curious if disabling wp_attachment_pages_enabled still serves a purpose. Will you include it in this plugin?

I don't think this is necessary because only thing this changes is the redirection logic. This plugin bypasses that completely because I want to show a 404 page instead.

vinkla commented 11 months ago

Great! Keep up the good work Johannes 🙌