add_query_arg() WordPress Function

The add_query_arg() function is used to add query strings to a URL in WordPress. This function is useful for adding custom variables to a URL, for example, when tracking referrals. Query strings are added to the end of a URL, after a question mark. Each query string consists of a key and a value, separated by an equals sign. Multiple query strings are separated by ampersands. The add_query_arg() function accepts two arguments: the key and the value. The key is the name of the custom variable, and the value is the value of the variable. For example, to add a query string with the key "ref" and the value "123", you would use the following code: add_query_arg( 'ref', '123', 'http://example.com' ); This would add the following query string to the URL: http://example.com/?ref=123 You can also use the add_query_arg() function to add multiple query strings to a URL. For example, the following code would add two query strings to the URL: add_query_arg( array( 'ref' => '123', 'source' => 'newsletter' ), 'http://example.com' ); This would add the following query strings to the URL: http://example.com/?ref=123&source=newsletter

add_query_arg( $args ) #

Retrieves a modified URL query string.


Description

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', 'http://example.com' );

Using an associative array:

add_query_arg( array(
    'key1' => 'value1',
    'key2' => 'value2',
), 'http://example.com' );

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.


Top ↑

Parameters

$key

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

$value

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

$url

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


Top ↑

Return

(string) New URL query string (unescaped).


Top ↑

More Information

Top ↑

Usage

// Parameters as separate arguments
add_query_arg( $param1, $param2, $old_query_or_uri );

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

Top ↑

Source

File: wp-includes/functions.php

function add_query_arg( ...$args ) {
	if ( is_array( $args[0] ) ) {
		if ( count( $args ) < 2 || false === $args[1] ) {
			$uri = $_SERVER['REQUEST_URI'];
		} else {
			$uri = $args[1];
		}
	} else {
		if ( count( $args ) < 3 || false === $args[2] ) {
			$uri = $_SERVER['REQUEST_URI'];
		} else {
			$uri = $args[2];
		}
	}

	$frag = strstr( $uri, '#' );
	if ( $frag ) {
		$uri = substr( $uri, 0, -strlen( $frag ) );
	} else {
		$frag = '';
	}

	if ( 0 === stripos( $uri, 'http://' ) ) {
		$protocol = 'http://';
		$uri      = substr( $uri, 7 );
	} elseif ( 0 === stripos( $uri, 'https://' ) ) {
		$protocol = 'https://';
		$uri      = substr( $uri, 8 );
	} else {
		$protocol = '';
	}

	if ( strpos( $uri, '?' ) !== false ) {
		list( $base, $query ) = explode( '?', $uri, 2 );
		$base                .= '?';
	} elseif ( $protocol || strpos( $uri, '=' ) === false ) {
		$base  = $uri . '?';
		$query = '';
	} else {
		$base  = '';
		$query = $uri;
	}

	wp_parse_str( $query, $qs );
	$qs = urlencode_deep( $qs ); // This re-URL-encodes things that were already in the query string.
	if ( is_array( $args[0] ) ) {
		foreach ( $args[0] as $k => $v ) {
			$qs[ $k ] = $v;
		}
	} else {
		$qs[ $args[0] ] = $args[1];
	}

	foreach ( $qs as $k => $v ) {
		if ( false === $v ) {
			unset( $qs[ $k ] );
		}
	}

	$ret = build_query( $qs );
	$ret = trim( $ret, '?' );
	$ret = preg_replace( '#=(&|$)#', '$1', $ret );
	$ret = $protocol . $base . $ret . $frag;
	$ret = rtrim( $ret, '?' );
	$ret = str_replace( '?#', '#', $ret );
	return $ret;
}


Top ↑

Changelog

Changelog
VersionDescription
5.3.0Formalized the existing and already documented parameters by adding ...$args to the function signature.
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More