Builds a URL query based on an associative or indexed array.
Description
This is a convenient function for easily building URL queries.
It sets the separator to ‘&’ and uses the _http_build_query() function.
Unlike PHP’s native http_build_query(), this function does NOT URL-encode the keys or values. Callers are responsible for encoding values beforehand with urlencode() or rawurlencode(), or late-escaping the output with esc_url() before use.
See also
- _http_build_query(): Used to build the query.
Parameters
$dataarrayrequired- Array of key/value pairs to build the query from.
Source
function build_query( $data ) {
return _http_build_query( $data, null, '&', '', false );
}
Changelog
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |
It’s not clearly spelled out that this function will call _http_build_query() with urlencode = FALSE. So it is assumed that you had previously urlencoded each individual key and value of your input array!
Crucially:
will output:
p%s/n#q?a*e!s p+=percent%slash/number#question?asterisk*exclamate!space plus+endIf you have an array with ‘raw’ data, you should use the native PHP function instead:
will output the proper/usable:
p%25s%2Fn%23q%3Fa%2Ae%21s+p%2B=percent%25slash%2Fnumber%23question%3Fasterisk%2Aexclamate%21space+plus%2Bend