Codex

Function Reference/shortcode atts

Contents

Description

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.

Usage

<?php shortcode_atts$pairs $atts ); ?>

Parameters

$pairs
(array) (required) Entire list of supported attributes and their defaults
Default: None
$atts
(array) (required) User defined attributes in shortcode tag
Default: None

Return Values

(array) 
Combined and filtered attribute list.

Examples

// [bartag foo="foo-value"]
function bartag_func($atts) {
	extract(shortcode_atts(array(
		'foo' => 'no foo',
		'bar' => 'default bar'
	), $atts));

	return "foo = {$foo}";
}
add_shortcode('bartag', 'bartag_func');

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.

Notes

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.

Change Log

Since: 2.5

Source File

shortcode_atts() is located in wp-includes/shortcodes.php.

Related

wp_parse_args()

Shortcode: do_shortcode(), add_shortcode(), remove_shortcode(), remove_all_shortcodes(), shortcode_atts(), strip_shortcodes()

See also index of Function Reference and index of Template Tags.