WordPress功能函數(shù)add_query_arg()
wordpress功能函數(shù)add_query_arg(),檢索修改后的URL查詢字符串。
用法:
add_query_arg( $args )
描述
您可以使用此函數(shù)重新生成URL,并將查詢變量追加到URL查詢。有兩種方式使用這個(gè)函數(shù);可以是單個(gè)鍵和值,也可以是關(guān)聯(lián)數(shù)組。
使用單個(gè)鍵和值:
add_query_arg( 'key', 'value', 'http://example.com' );
使用關(guān)聯(lián)數(shù)組:
add_query_arg( array(
'key1' => 'value1',
'key2' => 'value2',
), 'http://example.com' );
從任何一個(gè)使用中忽略URL都會(huì)導(dǎo)致當(dāng)前使用的URL ($_SERVER['REQUEST_URI']的值)。
值應(yīng)該使用urlencode()或rawurlencode()進(jìn)行適當(dāng)編碼。
將任何查詢變量的值設(shè)置為布爾值false將刪除鍵(請參閱remove_query_arg())。
重要提示:add_query_arg()的返回值默認(rèn)情況下不轉(zhuǎn)義。輸出應(yīng)該使用esc_url()或類似的方法進(jìn)行后期轉(zhuǎn)義,以幫助防止跨站點(diǎn)腳本攻擊(XSS)的漏洞。
參數(shù):
$key
(string|array) (必需) 查詢變量鍵或查詢變量的關(guān)聯(lián)數(shù)組。
$value
(string) (可選) 可以是查詢變量值,也可以是要操作的URL。
$url
(string) (可選) 要根據(jù)的URL。
返回
(string)新的URL查詢字符串(未轉(zhuǎn)義)。
更多信息
使用:
// 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
);
來源:
文件: 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, '?' );
return $ret;
}
更新日志:

用戶貢獻(xiàn)的筆記
(由Codex - 6年前貢獻(xiàn))
假設(shè)我們在WordPress的URL“http://blog.example.com/client/?”請注意在輸出鏈接之前使用了esc_url()。這是必要的,因?yàn)樵摵瘮?shù)不轉(zhuǎn)義url,如果輸出不轉(zhuǎn)義,將使頁面容易受到XSS腳本的攻擊。
// 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 ) );
(Ahmad Awais于5年前貢獻(xiàn))
安全地將用戶重定向到plugins.php中的自定義頁面
// Redirect to Welcome Page.
// Redirects to your-domain.com/wp-admin/plugin.php?page=your_plugin_page.
wp_safe_redirect( add_query_arg( array( 'page' => 'your_plugin_page' ), admin_url( 'plugins.php' ) ) );
(由Codex - 6年前貢獻(xiàn))
由于get_permalink()返回一個(gè)完整的URL,所以當(dāng)您想要向post的頁面添加變量時(shí),可以使用它。
/*
* 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 ) ) );
(由Codex - 6年前貢獻(xiàn))
通過關(guān)聯(lián)數(shù)組移除值和設(shè)置:
$query = 'http://example.com/link?foo=bar';
$new_query = add_query_arg( array(
'foo' => false,
'baz' => 'qux'
), $query );
print( $new_query );
// http://example.com/link?baz=qux
(由Codex - 6年前貢獻(xiàn))
通常情況下,您可能會(huì)發(fā)現(xiàn)自己在當(dāng)前頁面中使用以下方法創(chuàng)建url。在這些情況下,可以使用希望影響的URL作為最后一個(gè)參數(shù)。這里不需要使用esc_url(),因?yàn)檫@個(gè)值是安全的。
// This would output 'http://blog.example.com/2009/04/16/?hello=world'
echo esc_url( add_query_arg( 'hello', 'world', 'http://blog.example.com/2009/04/16/' ) );
(由gardelin貢獻(xiàn)- 2個(gè)月前)
如果查詢字符串值以==結(jié)束,則去掉一個(gè)=。
add_query_arg( array( 'something' => 'blabla==' ), 'https://www.google.com' );
結(jié)果將是
https://www.google.com?something=blabla=
但是應(yīng)該是
https://www.google.com?something=blabla==
(由janw貢獻(xiàn)。5年前)
一種使用add_query_arg獲取當(dāng)前總url的方法
home_url( add_query_arg( null, null ));