add_query_arg() wordpress function and usage This text is not used because the data attribute has a value

add_query_arg() wordpress function and usage

Home / WordPress / WordPress Wiki / add_query_arg() wordpress function and usage

add_query_arg( $args )
Retrieves a modified URL query string.


You can rebuild the URL and append query variables to the URL query by using this function. There are two ways to use this function; either a single key and value, or an associative array.

Using a single key and value:

add_query_arg( 'key', 'value', '' );

Using an associative array:

add_query_arg( array(
    'key1' => 'value1',
    'key2' => 'value2',
), '' );
Omitting the URL from either use results in the current URL being used (the value of $_SERVER['REQUEST_URI']).

Values are expected to be encoded appropriately with urlencode() or rawurlencode().

Setting any query variable’s value to boolean false removes the key (see remove_query_arg()).

Important: The return value of add_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.



(string|array) (Required) Either a query variable key, or an associative array of query variables.


(string) (Optional) Either a query variable value, or a URL to act upon.


(string) (Optional) A URL to act upon.


(string) New URL query string (unescaped).

More Information


// Parameters as separate arguments
add_query_arg( $param1, $param2, $old_query_or_uri );
// Parameters as array of key => value pairs
        'key1' => 'value1',
        'key2' => 'value2',

Sample Usage

Assuming we’re at the WordPress URL “”… Note the use of esc_url() before outputting the link. This is necessary because this function does not escape URLs and if output without escaping, would make the page vulnerable to XSS scripting.

// This would output '/client/?s=word&foo=bar'
echo esc_url( add_query_arg( 'foo', 'bar' ) );
// This would output '/client/?s=word&foo=bar&baz=tiny'
$arr_params = array( 'foo' => 'bar', 'baz' => 'tiny' );
echo esc_url( add_query_arg( $arr_params ) );

To safely redirect user to a custom page inside plugins.php

// Redirect to Welcome Page.
// Redirects to
wp_safe_redirect( add_query_arg( array( 'page' => 'your_plugin_page' ), admin_url( 'plugins.php' ) ) );

Since get_permalink() returns a full URL, you could use that when you want to add variables to a post’s page.

 * This would output whatever the URL to post ID 9 is, with 'hello=there'
 * appended with either ? or &, depending on what's needed.
echo esc_url( add_query_arg( 'hello', 'there', get_permalink( 9 ) ) );

More often than not you’ll probably find yourself creating URLs using the following method within the page you’re currently on. In these cases you can use the URL you want to affect as the last parameter. The use of esc_url() is not required here, because the value is known to be safe.

// This would output ''
echo esc_url( add_query_arg( 'hello', 'world', '' ) );

Removing values and setting via an associative array:

$query = '';
$new_query = add_query_arg( array(
    'foo' => false,
    'baz' => 'qux'
), $query );
print( $new_query );


0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments