bp_parse_args

Merge user defined arguments into defaults array.

Description

(array) bp_parse_args( (string|array) $args, (array) $defaults = array(), (string) $filter_key = '' ); 

This function is used throughout BuddyPress to allow for either a string or array to be merged into another array. It is identical to wp_parse_args() except it allows for arguments to be passively or aggressively filtered using the optional $filter_key parameter. If no $filter_key is passed, no filters are applied.

Returns (array)

Merged user defined values with defaults.

Parameters (3)

0. $args (string|array)
Value to merge with $defaults.
1. $defaults — Optional. (array) => array()
Array that serves as the defaults.
2. $filter_key — Optional. (string) => ''
String to key the filters from.

Usage

  1. if ( !function_exists( 'bp_parse_args' ) ) { 
  2. require_once '/bp-core/bp-core-functions.php'; 
  3.  
  4. // Value to merge with $defaults. 
  5. $args = null; 
  6.  
  7. // Array that serves as the defaults. 
  8. $defaults = array(); 
  9.  
  10. // String to key the filters from. 
  11. $filter_key = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_parse_args($args, $defaults, $filter_key); 
  15.  

Defined (1)

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

/bp-core/bp-core-functions.php  
  1. function bp_parse_args( $args, $defaults = array(), $filter_key = '' ) { 
  2.  
  3. // Setup a temporary array from $args. 
  4. if ( is_object( $args ) ) { 
  5. $r = get_object_vars( $args ); 
  6. } elseif ( is_array( $args ) ) { 
  7. $r =& $args; 
  8. } else { 
  9. wp_parse_str( $args, $r ); 
  10.  
  11. // Passively filter the args before the parse. 
  12. if ( !empty( $filter_key ) ) { 
  13.  
  14. /** 
  15. * Filters the arguments key before parsing if filter key provided. 
  16. * This is a dynamic filter dependent on the specified key. 
  17. * @since 2.0.0 
  18. * @param array $r Array of arguments to use. 
  19. */ 
  20. $r = apply_filters( 'bp_before_' . $filter_key . '_parse_args', $r ); 
  21.  
  22. // Parse. 
  23. if ( is_array( $defaults ) && !empty( $defaults ) ) { 
  24. $r = array_merge( $defaults, $r ); 
  25.  
  26. // Aggressively filter the args after the parse. 
  27. if ( !empty( $filter_key ) ) { 
  28.  
  29. /** 
  30. * Filters the arguments key after parsing if filter key provided. 
  31. * This is a dynamic filter dependent on the specified key. 
  32. * @since 2.0.0 
  33. * @param array $r Array of parsed arguments. 
  34. */ 
  35. $r = apply_filters( 'bp_after_' . $filter_key . '_parse_args', $r ); 
  36.  
  37. // Return the parsed results. 
  38. return $r;