build_query( array $data )

Build URL query based on an associative and, or indexed array.


Description Description

This is a convenient function for easily building url queries. It sets the separator to ‘&’ and uses _http_build_query() function.

See also See also


Top ↑

Parameters Parameters

$data

(array) (Required) URL-encode key/value pairs.


Top ↑

Return Return

(string) URL-encoded string.


Top ↑

Source Source

File: wp-includes/functions.php

function build_query( $data ) {
	return _http_build_query( $data, null, '&', '', false );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Andy Schmidt

    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:

    $myarray = array( 'p%s/n#q?a*e!s p+' =>  'percent%slash/number#question?asterisk*exclamate!space plus+end' );
    build_query( $myarray );

    will output:
    p%s/n#q?a*e!s p+=percent%slash/number#question?asterisk*exclamate!space plus+end

    If you have an array with ‘raw’ data, you should use the native PHP function instead:

    $myarray = array( 'p%s/n#q?a*e!s p+' =>  'percent%slash/number#question?asterisk*exclamate!space plus+end' );
    http_build_query( $myarray );

    will output the proper/usable:
    p%25s%2Fn%23q%3Fa%2Ae%21s+p%2B=percent%25slash%2Fnumber%23question%3Fasterisk%2Aexclamate%21space+plus%2Bend

You must log in before being able to contribute a note or feedback.