Friday, October 09, 2009

Feature Of The Day: The WordPress Shortcode API

I'm officially a huge fan of the WordPress Shortcode API. A short code is just what the name suggests - some abbreviated phrase that expands to a bunch of HTML. A classic example is the video plugin that allows you to say:

 [youtube id="Yu_moia-oVI"]

Versus having to enter in:

<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/KT--CsBh5TM&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/KT--CsBh5TM&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>

Short codes have been around in WordPress forever, and they've always been fairly easy to write. But the Shortcode API, makes them even easier to author, reduces chances you'll have to reinvent the wheel, and allows all your short codes to be consistent.

If you're thinking about learning how to author WordPress plugins, using the Shortcode API is an excellent place to start.

As a bonus, I learned about the extract function, which takes an associative array and ads them to your local namespace. So you can say:

 extract(array('foo' => 100, 'bar' => 'Hello World'));
 echo "$foo//$bar";

This is definitely a powerful function - it could either be an excellent way to do destructuring, or a terrific way to include a massive security hole in your application. Either way, it's big.

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails