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.

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: empty array

Changelog

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

Code of wp_parse_args() WP 5.5.1

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

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

From category: Helper Functions

No comments
    Log In . Register