shortcode_atts

Combine user attributes with known attributes and fill in defaults when needed.

Description

(array) shortcode_atts( (array) $pairs, (array) $atts, (string) $shortcode = '' ); 

The pairs should be considered to be all of the attributes which are supported by the caller and given as a list. The returned attributes will only contain the attributes in the $pairs list.

If the $atts list has unsupported attributes, then they will be ignored and removed from the final returned list.

Returns (array)

Combined and filtered attribute list.

Parameters (3)

0. $pairs (array)
Entire list of supported attributes and their defaults.
1. $atts (array)
User defined attributes in shortcode tag.
2. $shortcode — Optional. (string) => ''
The name of the shortcode, provided for context to enable filtering

Usage

  1. if ( !function_exists( 'shortcode_atts' ) ) { 
  2. require_once ABSPATH . WPINC . '/shortcodes.php'; 
  3.  
  4. // Entire list of supported attributes and their defaults. 
  5. $pairs = array(); 
  6.  
  7. // User defined attributes in shortcode tag. 
  8. $atts = array(); 
  9.  
  10. // Optional. The name of the shortcode, provided for context to enable filtering 
  11. $shortcode = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = shortcode_atts($pairs, $atts, $shortcode); 
  15.  

Defined (1)

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

/wp-includes/shortcodes.php  
  1. function shortcode_atts( $pairs, $atts, $shortcode = '' ) { 
  2. $atts = (array)$atts; 
  3. $out = array(); 
  4. foreach ($pairs as $name => $default) { 
  5. if ( array_key_exists($name, $atts) ) 
  6. $out[$name] = $atts[$name]; 
  7. else 
  8. $out[$name] = $default; 
  9. /** 
  10. * Filters a shortcode's default attributes. 
  11. * If the third parameter of the shortcode_atts() function is present then this filter is available. 
  12. * The third parameter, $shortcode, is the name of the shortcode. 
  13. * @since 3.6.0 
  14. * @since 4.4.0 Added the `$shortcode` parameter. 
  15. * @param array $out The output array of shortcode attributes. 
  16. * @param array $pairs The supported attributes and their defaults. 
  17. * @param array $atts The user defined shortcode attributes. 
  18. * @param string $shortcode The shortcode name. 
  19. */ 
  20. if ( $shortcode ) { 
  21. $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode ); 
  22.  
  23. return $out;