wp_parse_args

Merge user defined arguments into defaults array.

Description

(array) wp_parse_args( (string|array|object) $args, (string) $defaults = '' ); 

This function is used throughout WordPress to allow for both string or array to be merged into another array.

Returns (array)

Merged user defined values with defaults.

Parameters (2)

0. $args (string|array|object)
Value to merge with $defaults.
1. $defaults — Optional. (string) => ''
Array that serves as the defaults. Default empty.

Usage

  1. if ( !function_exists( 'wp_parse_args' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // Value to merge with $defaults. 
  5. $args = null; 
  6.  
  7. // Optional. Array that serves as the defaults. Default empty. 
  8. $defaults = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_parse_args($args, $defaults); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function wp_parse_args( $args, $defaults = '' ) { 
  2. if ( is_object( $args ) ) 
  3. $r = get_object_vars( $args ); 
  4. elseif ( is_array( $args ) ) 
  5. $r =& $args; 
  6. else 
  7. wp_parse_str( $args, $r ); 
  8.  
  9. if ( is_array( $defaults ) ) 
  10. return array_merge( $defaults, $r ); 
  11. return $r;