_remove_qs_args_if_not_in_url

Removes arguments from a query string if they are not present in a URL DO NOT use this in plugin code.

Description

(string) _remove_qs_args_if_not_in_url( (string) $query_string, (array) array $args_to_check, (string) $url ); 

Returns (string)

The altered query string

Parameters (3)

0. $query_string (string)
The query string.
1. $args_to_check (array) => array $args_to_check
The args to check.
2. $url (string)
The url.

Usage

  1. if ( !function_exists( '_remove_qs_args_if_not_in_url' ) ) { 
  2. require_once ABSPATH . WPINC . '/canonical.php'; 
  3.  
  4. // The query string. 
  5. $query_string = ''; 
  6.  
  7. // The args to check. 
  8. $args_to_check = array(); 
  9.  
  10. // The url. 
  11. $url = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = _remove_qs_args_if_not_in_url($query_string, $args_to_check, $url); 
  15.  

Defined (1)

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

/wp-includes/canonical.php  
  1. function _remove_qs_args_if_not_in_url( $query_string, Array $args_to_check, $url ) { 
  2. $parsed_url = @parse_url( $url ); 
  3. if ( ! empty( $parsed_url['query'] ) ) { 
  4. parse_str( $parsed_url['query'], $parsed_query ); 
  5. foreach ( $args_to_check as $qv ) { 
  6. if ( !isset( $parsed_query[$qv] ) ) 
  7. $query_string = remove_query_arg( $qv, $query_string ); 
  8. } else { 
  9. $query_string = remove_query_arg( $args_to_check, $query_string ); 
  10. return $query_string;