WordPress.org

Make WordPress Core

Ticket #23367: 23367.5.diff

File 23367.5.diff, 1.6 KB (added by morganestes, 7 years ago)

Put it all into a single function since it's limited use.

  • src/wp-admin/admin.php

    diff --git src/wp-admin/admin.php src/wp-admin/admin.php
    index 22813cc..9448ac0 100644
    if ( isset($_GET['import']) && !defined('WP_LOAD_IMPORTERS') ) 
    3030
    3131require_once(dirname(dirname(__FILE__)) . '/wp-load.php');
    3232
     33/**
     34 * Remove single-use URL parameters and create canonical link based on new URL.
     35 *
     36 * Remove specific query string parameters from a URL, create the canonical link,
     37 * put it in the admin header, and change the current URL to match.
     38 *
     39 * @since 4.2.0
     40 */
     41function wp_admin_canonical_url() {
     42        $removable_url_params = array(
     43                'message', 'updated', 'settings-updated',
     44                'saved', 'activated', 'activate',
     45                'deactivate', 'locked', 'skipped',
     46                'deleted', 'trashed', 'untrashed',
     47        );
     48
     49        /**
     50         * Filter the list of URL parameters to remove.
     51         *
     52         * @since 4.2.0
     53         *
     54         * @param array $removable_url_params An array of parameters to remove from the URL.
     55         */
     56        $removable_url_params = apply_filters( 'removable_url_params', $removable_url_params );
     57
     58        // Ensure we're using an absolute URL.
     59        $current_url  = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
     60        $filtered_url = remove_query_arg( $removable_url_params, $current_url );
     61        ?>
     62        <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
     63        <script>if ( window.history.replaceState ) {
     64                        window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href );
     65                }</script>
     66<?php
     67}
     68
     69add_action( 'admin_head', 'wp_admin_canonical_url' );
     70
    3371nocache_headers();
    3472
    3573if ( get_option('db_upgraded') ) {