remove_query_arg()
Removes the specified GET request parameters from the URL: ?parametr=value¶m2=val2.
Uses: add_query_arg()
1 time — 0.000055 sec (very fast) | 50000 times — 0.19 sec (very fast) | PHP 7.1.5, WP 4.8.2
No Hooks.
Returns
String. A new processed URL.
Usage
remove_query_arg( $key, $query );
- $key(string/array) (required)
- The parameter or parameters of the request that need to be removed from the URL.
- $query(boolean)
- URL, the string to be processed, from which the parameters specified in the first parameter should be removed. The current request is used by default: $_SERVER['REQUEST_URI'].
Default: false
Examples
#1 Example of deleting URL parameters
// Assuming we’re at the WordPress URL // http://www.example.com/client/?details=value1&type=value2&date=value3 $arr_params = array( 'details', 'type', 'date' ); echo esc_url( remove_query_arg( $arr_params ) ); // output: http://www.example.com/client/
Note the use of esc_url() before outputting the link.
#2 Deleting several request parameters from the URL
Suppose we pass the query parameters remove_recomend and post_id, process them and then we need a link without these parameters to use it further in the code:
// uses $_SERVER['REQUEST_URI'] and the passed query parameters // delete the parameters that are no longer needed $page_url = remove_query_arg( [ 'remove_recomend', 'post_id' ] ); // use the $page_url variable cleared of parameters echo esc_url( $page_url );
#3 Remove request parameters from the specified URL
$url = 'http://blog.example.com/2009/?hello=world&foo=bar'; echo remove_query_arg( 'hello', $url ); //> http://blog.example.com/2009/?foo=bar
Changelog
| Since 1.5.0 | Introduced. |
remove_query_arg() remove query arg code WP 7.0
function remove_query_arg( $key, $query = false ) {
if ( is_array( $key ) ) { // Removing multiple keys.
foreach ( $key as $k ) {
$query = add_query_arg( $k, false, $query );
}
return $query;
}
return add_query_arg( $key, false, $query );
}