Codex tools: Log in
Contents |
Combines user shortcode attributes with known attributes and fills in defaults when needed. The result will contain every key from the known attributes, merged with values from shortcode attributes.
<?php shortcode_atts( $pairs , $atts ); ?>
function bartag_func($atts) {
extract(shortcode_atts(array(
'foo' => 'no foo',
'bar' => 'default bar'
), $atts));
return 'bartag: ' . $foo . ' ' . $bar;
}
add_shortcode('bartag', 'bartag_func');
[bartag foo="koala" bar="bears"] outputs the following:
bartag: koala bears
This creates a "[bartag]" shortcode that supports two attributes: [bartag foo="something" bar="something else"]. Both attributes are optional and will take on default options if they are not provided. Note that using extract method here turns each key in the merged array into its own variable such as $foo and $bar for easy access. The $atts or the default array will not be modified after the call to shortcode_atts.
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.
Since: 2.5
shortcode_atts() is located in wp-includes/shortcodes.php.
Shortcode: do_shortcode(), add_shortcode(), remove_shortcode(), remove_all_shortcodes(), shortcode_atts(), strip_shortcodes(), shortcode_exists(), has_shortcode(), wp_audio_shortcode(), wp_video_shortcode()