Make WordPress Core


Ignore:
Timestamp:
09/22/2023 12:02:28 AM (19 months ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/canonical.php

    r56559 r56657  
    1414        parent::set_up();
    1515        wp_set_current_user( self::$author_id );
     16
     17        add_filter( 'pre_option_wp_attachment_pages_enabled', '__return_true' );
    1618    }
    1719
     
    403405        $this->assertNull( $url );
    404406    }
     407
     408    /**
     409     * @ticket 57913
     410     */
     411    public function test_canonical_attachment_page_redirect_with_option_disabled() {
     412        add_filter( 'pre_option_wp_attachment_pages_enabled', '__return_false' );
     413
     414        $filename = DIR_TESTDATA . '/images/test-image.jpg';
     415        $contents = file_get_contents( $filename );
     416        $upload   = wp_upload_bits( wp_basename( $filename ), null, $contents );
     417
     418        $attachment_id   = $this->_make_attachment( $upload );
     419        $attachment_page = get_permalink( $attachment_id );
     420
     421        $this->go_to( $attachment_page );
     422
     423        $url      = redirect_canonical( $attachment_page, false );
     424        $expected = wp_get_attachment_url( $attachment_id );
     425
     426        $this->assertSame( $expected, $url );
     427    }
    405428}
Note: See TracChangeset for help on using the changeset viewer.