Skip to content

Commit

Permalink
Media: Disable attachment pages for new installations.
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://develop.svn.wordpress.org/trunk@56657 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Sep 22, 2023
1 parent e486ac0 commit 15e3700
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/wp-admin/includes/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/wp-admin/includes/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions src/wp-includes/canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = '';
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @global int $wp_db_version
*/
$wp_db_version = 55853;
$wp_db_version = 56657;

/**
* Holds the TinyMCE version.
Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit/tests/canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Tests_Canonical extends WP_Canonical_UnitTestCase {
public function set_up() {
parent::set_up();
wp_set_current_user( self::$author_id );

add_filter( 'pre_option_wp_attachment_pages_enabled', '__return_true' );
}

/**
Expand Down Expand Up @@ -402,4 +404,25 @@ public function test_feed_canonical_with_not_exists_query() {

$this->assertNull( $url );
}

/**
* @ticket 57913
*/
public function test_canonical_attachment_page_redirect_with_option_disabled() {
add_filter( 'pre_option_wp_attachment_pages_enabled', '__return_false' );

$filename = DIR_TESTDATA . '/images/test-image.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );

$attachment_id = $this->_make_attachment( $upload );
$attachment_page = get_permalink( $attachment_id );

$this->go_to( $attachment_page );

$url = redirect_canonical( $attachment_page, false );
$expected = wp_get_attachment_url( $attachment_id );

$this->assertSame( $expected, $url );
}
}
2 changes: 2 additions & 0 deletions tests/phpunit/tests/canonical/postStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
public function set_up() {
parent::set_up();
self::setup_custom_types();

add_filter( 'pre_option_wp_attachment_pages_enabled', '__return_true' );
}

/**
Expand Down

0 comments on commit 15e3700

Please sign in to comment.