WordPress: wp_parse_args ()

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 ()

4 comments to "WordPress: wp_parse_args ()"

  1. January 5th, 2009 WordPress: wp_parse_args () | Undolog.com:

    [...] Reading the source of this article: WordPress: wp_parse_args () | Undolog.com Author: [...]

  2. January 5th, 2009 WordPress: wp_parse_args () | Undolog.com :

    [...] See original article further: WordPress: wp_parse_args () | Undolog.com Author: [...]

  3. July 3, 2009 Undolog.com »Wordpress: use shortcodes :

    [...] To WordPress as we have seen: wp_parse_args (), the management of attributes is very simple and allows you to set default values ​​in [...]

  4. November 25, 2011 Giovambattista Fazioli :

    update: WordPress has updated the documentation of this function.

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA