| 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 | */ |
| 41 | function wp_admin_canonical_url() { |
| 42 | $removable_query_args = array( |
| 43 | 'message', 'settings-updated', 'saved', |
| 44 | 'update', 'updated','activated', |
| 45 | 'activate', 'deactivate', 'locked', |
| 46 | 'deleted', 'trashed', 'untrashed', |
| 47 | 'enabled', 'disabled', 'skipped', |
| 48 | ); |
| 49 | |
| 50 | /** |
| 51 | * Filter the list of URL parameters to remove. |
| 52 | * |
| 53 | * @since 4.2.0 |
| 54 | * |
| 55 | * @param array $removable_query_args An array of parameters to remove from the URL. |
| 56 | */ |
| 57 | $removable_query_args = apply_filters( 'removable_query_args', $removable_query_args ); |
| 58 | |
| 59 | // Ensure we're using an absolute URL. |
| 60 | $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); |
| 61 | $filtered_url = remove_query_arg( $removable_query_args, $current_url ); |
| 62 | ?> |
| 63 | <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" /> |
| 64 | <?php |
| 65 | if ( empty( $removable_query_args ) ) { |
| 66 | return; |
| 67 | } |
| 68 | ?> |
| 69 | <script>if ( window.history.replaceState ) { |
| 70 | window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href ); |
| 71 | }</script> |
| 72 | <?php |
| 73 | } |
| 74 | |
| 75 | add_action( 'admin_head', 'wp_admin_canonical_url' ); |
| 76 | |