wp_parse_args()将用户提供的数组与默认值合并

目录

描述

wp_parse_args is a generic utility for merging together an array of arguments and an array of default values. It can also be given a URL query type string which will be converted into an array (i.e. "id=5&status=draft").

It is used throughout WordPress to avoid having to worry about the logic of defaults and input, and produces a stable pattern for passing arguments around. Functions like query_posts, wp_list_comments and get_terms are common examples of the simplifying power of wp_parse_args.

Functions that have an $args based setting are able to infinitely expand the number of values that can potentially be passed into them, avoiding the annoyance of super-long function calls because there are too many arguments to keep track of, many of whose only function is to override usually-good defaults on rare occasions.

wp_parse_args() 描述

用法

wp_parse_args() is used inside a function you are defining to process incoming arguments. Below is an example of how wp_parse_args() can merge an array of defaults with an array of mixed arguments.

wp_parse_args() 用法

参数

$args

(array/string) (必填) Query string or Array of mixed arguments that will override the values in $defaults.

默认值: None

Can be passed URL-query style

Or as an array definition

$defaults

(array) (必填) Array of default values that will be overridden by $args

默认值: None

wp_parse_args() 参数

历史

添加于 版本: 2.2.0

wp_parse_args() 历史

源文件

wp_parse_args() 函数的代码位于 wp-includes/functions.php.

wp_parse_args() 源文件

相关