Detailed description of the wp_parse_args () function used to synthesize arrays in WordPress

Source: Internet
Author: User
This article mainly introduces the wp_parse_args () function used to synthesize arrays in WordPress, which is usually used to facilitate query. For more information, see wp_parse_args () A function is a commonly used function in the WordPress core. it is used for many purposes, but is mainly used to bind a default value to an array parameter (args.

Because the wp_parse_args () function must return an array, it will automatically convert the input query string and object into an array, giving users more convenient conditions, added compatibility.

Common query_posts (), wp_list_comments (), and get_terms () functions use the wp_parse_args () function to add default values to array parameters.

Usage

wp_parse_args( $args, $defaults );

Parameters

$ Args

(Array | string) (required) query string, object, or array parameter, used to bind 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) default parameter of the array parameter.

Default value: Null string

Example

Function explain_parse_args ($ args = array () {// $ default value of args $ defaults = array ('before' =>'

', 'After' =>'

', 'Echo' => true, 'text' => 'WP _ parse_args () function Demo'); // bind the default value $ r = wp_parse_args ($ args, $ defaults); $ output = $ r ['before']. $ r ['text']. $ r ['after']; if (! $ R ['echo ']) return $ output; echo $ output;} // no parameter explain_parse_args (); // Print:

Wp_parse_args () function demonstration

// String parameter $ output = explain_parse_args ('text = string parameter & before =

& Echo = 0'); echo $ output; // Print:

String parameters

// Array parameter explain_parse_args (array ('text' => 'Array parameter', 'before' =>'

'); // Print:

Array parameters

Another way to avoid using the second $ ULTS parameter is to directly convert the variables of a query string, object, or array to a common 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 = 'object (object )'; function func () {// when converting to an array, the functions in the object will be 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
The source code of the wp_parse_args function is relatively simple,
Depending on the PHP built-in functions get_object_vars, array_merge, and WordPress wp_parse_str,
The source code of the function is as follows:

/*** Merge user defined arguments into defaults array. ** This function is used throughout WordPress to allow for both string or array * to be 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 merge with $ defaults * The second parameter is the default array of values, it must be an array * @ param Array $ defaults array that serves as the defaults. * the returned value will be an array * @ return array Merged user defined values with defaults. */function wp_parse_args ($ args, $ defaults = '') {if (is_object ($ args) // the object to be received (obj) convert to array $ r = get_object_vars ($ args); elseif (is_array ($ args) // if it is an array, do not convert $ r = & $ args; else // Convert the received string to the array wp_parse_str ($ args, $ r); if (is_array ($ defaults) return array_merge ($ defaults, $ r ); return $ r ;}

The get_object_vars function is used to return an Associated array composed of object attributes.
The array_merge function combines two or more arrays. the values in an array are appended to the values of the previous array. Returns an array of results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.