From 229d32f6d705a1174d5d9b5d621f57d8130b93af Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 22 Sep 2023 00:04:41 +0000 Subject: [PATCH] Media: Disable attachment pages for new installations. WordPress creates attachment pages by default for every attachment uploaded. On the vast majority of sites, these attachment pages don't contain any meaningful information. They do however exist, get indexed by search engines, and sometimes even rank in search results, leading to bad results for users and site owners. This commit introduces a `wp_attachment_pages_enabled` database option to control the attachment pages behavior: * On existing sites, the option is set to `1` on upgrade, so that attachment pages continue to work as is. * For new sites, the option is set to to `0` by default, which means attachment pages are redirected to the attachment URL. * Sites that want to enable or disable the attachment pages can set the option to `1` or `0`, respectively. Follow-up to [2958], [3303], [7149], [34690]. Props aristath, poena, afercia, joostdevalk, jonoaldersonwp, azaozz, johnbillion, joedolson, basiliskan, audrasjb, davelo, rilwis, manfcarlo, tyxla, garrett-eclipse, seedsca, eatingrules, matveb, antpb, zodiac1978, oglekler, zunaid321, costdev, SergeyBiryukov. Fixes #57913. Built from https://develop.svn.wordpress.org/trunk@56657 git-svn-id: http://core.svn.wordpress.org/trunk@56169 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/schema.php | 3 +++ wp-admin/includes/upgrade.php | 21 +++++++++++++++++++++ wp-includes/canonical.php | 13 +++++++++++++ wp-includes/version.php | 4 ++-- 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/wp-admin/includes/schema.php b/wp-admin/includes/schema.php index 89d9fabfc0..d339af3b17 100644 --- a/wp-admin/includes/schema.php +++ b/wp-admin/includes/schema.php @@ -556,6 +556,9 @@ function populate_options( array $options = array() ) { // 5.8.0 'wp_force_deactivated_plugins' => array(), + + // 6.4.0 + 'wp_attachment_pages_enabled' => 0, ); // 3.3.0 diff --git a/wp-admin/includes/upgrade.php b/wp-admin/includes/upgrade.php index bf0b12405d..f4aac0e764 100644 --- a/wp-admin/includes/upgrade.php +++ b/wp-admin/includes/upgrade.php @@ -839,6 +839,10 @@ function upgrade_all() { upgrade_630(); } + if ( $wp_current_db_version < 56657 ) { + upgrade_640(); + } + maybe_disable_link_manager(); maybe_disable_automattic_widgets(); @@ -2322,6 +2326,23 @@ function upgrade_630() { } } +/** + * Executes changes made in WordPress 6.4.0. + * + * @ignore + * @since 6.4.0 + * + * @global int $wp_current_db_version The old (current) database version. + */ +function upgrade_640() { + global $wp_current_db_version; + + if ( $wp_current_db_version < 56657 ) { + // Enable attachment pages. + update_option( 'wp_media_use_attachment_pages', 1 ); + } +} + /** * Executes network-level upgrade routines. * diff --git a/wp-includes/canonical.php b/wp-includes/canonical.php index 606973b31d..2f3cd68903 100644 --- a/wp-includes/canonical.php +++ b/wp-includes/canonical.php @@ -545,6 +545,18 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { } } + $is_attachment_redirect = false; + + if ( is_attachment() && ! get_option( 'wp_attachment_pages_enabled' ) ) { + $attachment_id = get_query_var( 'attachment_id' ); + + if ( current_user_can( 'read_post', $attachment_id ) ) { + $redirect_url = wp_get_attachment_url( $attachment_id ); + + $is_attachment_redirect = true; + } + } + $redirect['query'] = preg_replace( '#^\??&*?#', '', $redirect['query'] ); // Tack on any additional query vars. @@ -650,6 +662,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { // Trailing slashes. if ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() + && ! $is_attachment_redirect && ! is_404() && ( ! is_front_page() || is_front_page() && get_query_var( 'paged' ) > 1 ) ) { $user_ts_type = ''; diff --git a/wp-includes/version.php b/wp-includes/version.php index 7c8e34c749..2920ba7159 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,14 +16,14 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56656'; +$wp_version = '6.4-alpha-56657'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * * @global int $wp_db_version */ -$wp_db_version = 55853; +$wp_db_version = 56657; /** * Holds the TinyMCE version.