The function wp_parse_args() (like many other undocumented) issue a string in the format:
1 | var1 = value1 & var2 = value2 ... Varn = valueN |
This is a very widespread in WordPress functions and is useful when a function supports multiple paramteri. As mentioned topics and default variables in Javascript, Actionscript and PHP , there is no need to pass parameters in the mode var1=value1&var2=value2...varn=valuen , since PHP is able to manage safely pass parameters variables.
Synopsis
1 2 3 4 5 6 7 8 9 10 11 12 | / ** * Merge user defined arguments into defaults array. * * This function is used throughout WordPress to allow for string or array Both * To be merged into another array. * * @ Since 2.2.0 * * @ Param (string | array) $ args Value to merge with $ defaults * @ Param (array) $ defaults Array That Serves as the defaults. * @ Return (array) Merged user defined values with defaults. * / |
In fact, the first parameter can also be an object. If we pass a string with values separated by ampersen, the function is called lower-level WordPress wp_parse_str()
Example
To clarify the operation we see an example. . Imagine having to write a function ( myFunc() ) that supports three optional parameters: par1, par2 e par3 . The statement will be of course:
1 | $args ) { } function myFunc ($ args) {} |
Where is our $ args string in the format:
1 | var1 = value1 & var2 = value2 ... Varn = valueN |
A possible call might be:
1 | ) ; myFunc ("PAR3 & par1 = hello = hello"); |
I purposely put the parameters in any order, another feature that makes it versatile. Let's see how the work so wp_parse_args() :
1 2 3 4 5 6 7 | $args ) { function myFunc ($ args) { wp_parse_args ( $args ) ; New_args wp_parse_args = $ ($ args); / / [ "par1" ] ; echo $ new_args ["par1"]; [ "par2" ] ; echo $ new_args ["par2"]; [ "par3" ] ; echo $ new_args ["PAR3"]; } |
non è impostato! Calling our function myFunc( "par3=ciao&par1=salve" ); find that par2 is not set! And that's why the second parameter of the function wp_parse_args() :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $args ) { function myFunc ($ args) { array ( "par1" => "salve" , $ Default = array ("par1" => "hello" "come" , "Par2" => "how" "va?" ) ; "PAR3" => "wrong?"); / / wp_parse_args ( $args , $default ) ; New_args wp_parse_args = $ ($ args, $ defaults); / / [ "par1" ] ; echo $ new_args ["par1"]; [ "par2" ] ; echo $ new_args ["par2"]; [ "par3" ] ; echo $ new_args ["PAR3"]; } / / ) ; myFunc ("PAR3 = you're = you & par1"); / / We / / You / / As / / You're |
The convenience of this approach is the ability to pre-set default values that are used when not in the string passed as the first parameter.
Why use wp_parse_args ()
e array_merge() . All this would be possible without using wp_parse_args() as PHP already provides all the functions to do this work, see for example: parse_str() and array_merge() . Use wp_parse_args() should mainly for 2 reasons:
- manages for us the passage of an object, an array or a string
- its behavior is modified through the filters of WordPress: see apply_filters ()










[...] Reading the source of this article: WordPress: wp_parse_args () | Undolog.com Author: [...]
[...] See original article further: WordPress: wp_parse_args () | Undolog.com Author: [...]
[...] To WordPress as we have seen: wp_parse_args (), the management of attributes is very simple and allows you to set default values in [...]
update: WordPress has updated the documentation of this function.