add_query_arg

函数


add_query_arg ( $args )
参数
  • (string|array)
    $key
    Either a query variable key, or an associative array of query variables.
    Required:
  • (string)
    $value
    Optional. Either a query variable value, or a URL to act upon.
    Required:
  • (string)
    $url
    Optional. A URL to act upon.
    Required:
返回值
  • (string) New URL query string (unescaped).
定义位置
  • wp-includes/functions.php
    , line 1130
引入
1.5.0
弃用

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’, ‘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.

function add_query_arg( ...$args ) {
	if ( is_array( $args[0] ) ) {
		if ( count( $args )  $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;
}