WordPress at a glance

wp_parse_args() WP 1.0

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.

Is the basis for: register_rest_field()
Works based on: wp_parse_str()
✈ 1 time = 0.000018s = very fast | 50000 times = 0.08s = speed of light | PHP 7.1.5, WP 4.8.2

No Hooks.

Return

Array. Merged user defined values with defaults.

Usage

wp_parse_args( $args, $defaults );
$args(string/array/object) (required)
Value to merge with $defaults.
$defaults(array)
Array that serves as the defaults.
Default: ''

Changelog

Since 2.2.0 Introduced.
Since 2.3.0 $args can now also be an object.

Code of wp parse args: wp-includes/functions.php WP 5.2.2

<?php
function wp_parse_args( $args, $defaults = '' ) {
	if ( is_object( $args ) ) {
		$r = get_object_vars( $args );
	} elseif ( is_array( $args ) ) {
		$r =& $args;
	} else {
		wp_parse_str( $args, $r );
	}

	if ( is_array( $defaults ) ) {
		return array_merge( $defaults, $r );
	}
	return $r;
}

Related Functions

From category: Helper Functions

No comments
    Hello, !     Log In . Register