The Wp_parse_args () function is a function that is often used by the WordPress core, and it uses a lot, but is mostly used to bind a default value to an array parameter (args).
Because the Wp_parse_args () function returns an array, it automatically converts the incoming query string and object (object) to a number of groups, giving the user more convenient conditions and increasing compatibility.
The common query_posts (), wp_list_comments (), and get_terms () functions use the Wp_parse_args () function to help it add default values to array parameters.
Usage
Wp_parse_args ($args, $defaults);
Parameters
$args
(Array | String) (must) query a string, object, or array parameter to bind to the default value.
Default value: None
Query string:
Type=post&posts_per_page=5&cat=1
Array:
Array (' type ' => ' post ', ' Posts_per_page ' => 5, ' Cat ' => ' 1 ')
$defaults
(array) (optional) The default parameters for the array parameter.
Default value: Empty string
Example
function Explain_parse_args ($args = Array ()) {//$args default value $defaults = Array (' Before ' => ' <div class= ')
Box ">", ' After ' => ' </div> ', ' echo ' => true, ' text ' => ' Wp_parse_args () function demo ');
Binding default Value $r = Wp_parse_args ($args, $defaults); $output = $r [' Before ']. $r [' Text '].
$r [' after '];
if (! $r [' echo ']) return $output;
Echo $output; //No Parameters Explain_parse_args ()//print: <div class= "box" >wp_parse_args () function demo </div>//string parameter $output = Explain_p
Arse_args (' text= string parameter &before=<div class= ' box-2 ' >&echo=0 '); echo $output//print: <div class= "Box-2" > String parameters </div>//Array Parameters Explain_parse_args (' text ' => ' array arguments ', ' Fore ' => ' <div class= ' box-3 ' > ');//print: <div class= "box-3" > Array parameters </div> There is another usage that does not use the second $defaults parameter.
is to help you convert a query string, object, or array of variables directly into a generic array to avoid judging the type.
String $array = Wp_parse_args (' text= test another usage &type= string ');
Var_dump ($array); /* Array (2) {["Text"]=> StriNg (21) "Test Another usage" ["Type"]=> string (9) "string"} *//object class args_obj{Public $text = ' Test another usage
';
Public $type = ' Objects (object) ';
function func () {//converting an array, the functions inside the object are ignored} $obj = new Args_obj;
Var_dump ($obj);
/* Object (Args_obj) #2175 (2) {["Text"]=> string (21) "Test Another usage" ["Type"]=> string (18) Object]} * *
Wp_parse_args function Source code detailed
Wp_parse_args function of the source code is relatively simple,
Attached to PHP built-in functions get_object_vars, Array_merge and WordPress wp_parse_str functions to achieve,
The following is the source code for this function:
/**
* Merge User defined arguments into defaults array.
*
* This function is used throughout the WordPress to allow for both string or array * to being
merged into another array .
* *
@since 2.2.0
* *
The first parameter can be a string, array, or object (obj)
* @param string|array $args Value to the merge with $defaults
* The second parameter is the default array of preset values, which must be an array * @param array $defaults array that serves as the
defaults.
* The return value will be an array
* @return Array merged user defined values with defaults.
*/
function Wp_parse_args ($args, $defaults = ') {
if (Is_object ($args))
//Converts the Received object (obj) to an array
$r = Get_object_vars ($args);
ElseIf (Is_array ($args))
//If the array is not converted
$r =& $args;
else
//Converts the received string to an array of
wp_parse_str ($args, $r);
if (Is_array ($defaults)) return
Array_merge ($defaults, $r);
return $r;
}
Where the Get_object_vars function is used to return an associative array consisting of object properties. The
Array_merge function is to combine cells of two or more arrays, and the values in one array are appended to the previous array. Returns an array as the result.