Anatomy Of A WordPress Plugin: Shortcode Extract Of Parameters

WordPressIn my last post I discussed adding a shortcode to the plugin and covered how to extract the parameters passed into the function from the shortcode.

There is one variation to this example which it is probably worth mentioning. In the last post I showed that the width parameter could be extracted using the following code:

extract(shortcode_atts(array(
		'width' => '75%'
	), $atts));

This extracts the parameter so it can be used as $width; if multiple parameters were extracted then they would all be used in a similar way.

However, you can also extract the parameters into an array for use:

$parameters = shortcode_atts(array(
		'width' => '75%'
	), $atts);

This would make the width parameter available by referencing the array:

return $parameters['width']};

I typically use the former approach, but using the latter would make it more readily apparent when the variables being used had been passed in from the shortcode or set within the function.

Click to show/hide the Anatomy Of A WordPress Plugin Series Index