wp_admin_canonical_url

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

Description

wp_admin_canonical_url(); 

Remove specific query string parameters from a URL, create the canonical link, put it in the admin header, and change the current URL to match.


Usage

  1. if ( !function_exists( 'wp_admin_canonical_url' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/misc.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_admin_canonical_url(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/misc.php  
  1. function wp_admin_canonical_url() { 
  2. $removable_query_args = wp_removable_query_args(); 
  3.  
  4. if ( empty( $removable_query_args ) ) { 
  5. return; 
  6.  
  7. // Ensure we're using an absolute URL. 
  8. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 
  9. $filtered_url = remove_query_arg( $removable_query_args, $current_url ); 
  10. ?> 
  11. <link id="wp-admin-canonical" rel="canonical" href="<?php echo esc_url( $filtered_url ); ?>" /> 
  12. <script> 
  13. if ( window.history.replaceState ) { 
  14. window.history.replaceState( null, null, document.getElementById( 'wp-admin-canonical' ).href + window.location.hash ); 
  15. </script> 
  16. <?php