Make WordPress Core


Ignore:
Timestamp:
03/11/2015 11:08:56 PM (12 years ago)
Author:
ocean90
Message:

Administration: Remove single-use URL parameters and create canonical link based on new URL.

The default removable query args are 'message', 'settings-updated', 'saved', 'update', 'updated','activated', 'activate', 'deactivate', 'locked', 'deleted', 'trashed', 'untrashed', 'enabled', 'disabled', and 'skipped'.

props morganestes.
fixes #23367.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/misc.php

    r30200 r31736  
    842842        }
    843843}
    844 
    845844add_action( 'post_edit_form_tag', 'post_form_autocomplete_off' );
     845
     846/**
     847 * Remove single-use URL parameters and create canonical link based on new URL.
     848 *
     849 * Remove specific query string parameters from a URL, create the canonical link,
     850 * put it in the admin header, and change the current URL to match.
     851 *
     852 * @since 4.2.0
     853 */
     854function wp_admin_canonical_url() {
     855        $removable_query_args = array(
     856                'message', 'settings-updated', 'saved',
     857                'update', 'updated','activated',
     858                'activate', 'deactivate', 'locked',
     859                'deleted', 'trashed', 'untrashed',
     860                'enabled', 'disabled', 'skipped',
     861        );
     862
     863        /**
     864         * Filter the list of URL parameters to remove.
     865         *
     866         * @since 4.2.0
     867         *
     868         * @param array $removable_query_args An array of parameters to remove from the URL.
     869         */
     870        $removable_query_args = apply_filters( 'removable_query_args', $removable_query_args );
     871
     872        if ( empty( $removable_query_args ) ) {
     873                return;
     874        }
     875
     876        // Ensure we're using an absolute URL.
     877        $current_url  = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
     878        $filtered_url = remove_query_arg( $removable_query_args, $current_url );
     879        ?>
     880        <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" />
     881        <script>
     882                if ( window.history.replaceState ) {
     883                        window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href );
     884                }
     885        </script>
     886<?php
     887}
     888add_action( 'admin_head', 'wp_admin_canonical_url' );
Note: See TracChangeset for help on using the changeset viewer.