WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The codex says

shortcode_atts() 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.

It doesn't make much sense to me (I'm a newbie).

Here is an example:

function wps_trend($atts) {
extract( shortcode_atts( array(
'w' => '500', 
'h' => '330',
'q' => '',
'geo' => 'US',
), $atts));
$h = (int) $h;
$w = (int) $w;
$q = esc_attr($geo);

Please can you explain?

share|improve this question
up vote 21 down vote accepted

shortcode_atts() works like array_merge(): It merges the second list of arguments into the first one. The difference is: It merges only keys present in the first argument ($default).

extract() then takes the array keys, sets these as variable names and their values as variable values. 'w' => '500' in your example becomes $w = '500'.

Do not use extract(). This very bad code style. Its usage was deprecated even in core, and that means something … :)

Your example should be written as:

$args = shortcode_atts( 
        'w'   => '500',
        'h'   => '330',
        'q'   => '',
        'geo' => 'US',
$w = (int) $args['w'];
$h = (int) $args['h'];
$q = esc_attr( $args['q'] );
share|improve this answer
Thank-you. I didn't know about the extract, so thanks for that too! – mattnewbie May 15 '13 at 20:07
extract() is also discouraged by WP coding standards. See make.wordpress.org/core/handbook/best-practices/… – alexg Feb 17 at 9:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.