• I have this code in a shortcode that I’m creating:

    extract( shortcode_atts( array(
    		'id' => '',
    		'size' => '1/2',
    		'group' => '',
    	), $atts ) );

    Everything seems to work fine but for some reason the shortcode_atts function is not setting up the defaults.

    So when I try something like:
    $size = $atts['size'];

    I get a PHP notice Notice: Undefined index: size in…

    I’m using the shortcode like this: [media id=81]

    Any ideas? (or known bugs?)

    I’m working on the latest version (3.3.1)

    I

Viewing 4 replies - 1 through 4 (of 4 total)
  • shortcode_atts() combines any user provided parameters ($atts) with the array of defaults you provide and returns the merged array.

    Function Reference/shortcode atts

    In your example above $atts['size'] doesn’t exist and thus results in an undefined index notice. You want to either store the returned array in a variable or (like you are currently doing) use PHP’s extract function to create individual variables from each element in the array. After using extract(), $size, $id, and $group exist and are already set to the proper values.

    Edit:
    Just a minor side note, there’s an errant comma in your code. Should be:

    'group' => ''

    Thread Starter moikirsch

    (@moikirsch)

    Hi Big Bagel,

    There shouldn’t be a notice since I’m setting up the default value for size to be ‘1/2’ even if the user doesn’t call it.

    Actually I think that the problem was related to the extract function.

    I was able to fix it using the following line instead:

    $atts = shortcode_atts( array( 'id' => '', 'size' => '1/2', 'group' => '', 'showtitle' => true, 'showicon' => true, 'showbody' => true, 'icon' => ''), $atts );

    Yes, there should be a notice. $atts is whatever parameters the user provides. When this shortcode is used, only $atts['id'] exists:

    [media id=81]

    The function shotcode_atts() then takes that array and merges it with the defaults you provide, returning the result. If you used:

    $merged = shortcode_atts( array( 'id' => '', 'size' => '1/2', 'group' => '', 'showtitle' => true, 'showicon' => true, 'showbody' => true, 'icon' => ''), $atts );

    Then $merged would be an array of all the proper values and $atts would still only be an array of whatever the user provided. Your new code in effect merges the given and default parameters then overwrites the $atts array with the new array. Using extract(), like you did before, automatically creates a variable that can be immediately used for each key in the returned array ($id, $size, $group, etc.).

    Some good shortcode references:

    Shortcode API
    http://planetozh.com/blog/2008/03/wordpress-25-shortcodes-api-overview/
    http://aaron.jorb.in/blog/2010/02/wordpress-shortcodes-a-how-to-by-example/#.TwyTGG9SQsI
    http://wp.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/
    http://wp.tutsplus.com/tutorials/getting-started-with-wordpress-shortcodes/

    Thanks for posting your solution, @moikirsch!

    I had the same problem, where the extract function (which appears in the Codex example) seems to serve no purpose.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘shortcode_atts defaults are not working’ is closed to new replies.