remove_query_arg()WP 1.5.0

Removes an item or items from a query string.

Important: The return value of remove_query_arg() is not escaped by default. Output should be late-escaped with esc_url() or similar to help prevent vulnerability to cross-site scripting (XSS) attacks.

1 time — 0.000055 sec (very fast) | 50000 times — 0.19 sec (very fast) | PHP 7.1.5, WP 4.8.2

No Hooks.

Return

String. New URL query string.

Usage

remove_query_arg( $key, $query );
$key(string|string[]) (required)
Query key or keys to remove.
$query(false|string)
When false uses the current URL.
Default: false

Examples

1

#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.

0

#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 );
0

#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() code WP 6.5.2

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 );
}