WordPress Shortcodes: what? why? how?

Posted: 9th Apr 2008, in: Snippets, plugins - Older Post - Newer Post

WordPress 2.5 implemented a new way of inserting content into posts: shortcodes. If you haven’t had the need to use the new gallery shortcode you might not have come across the details. So, what are they?

Plugin authors have used all sorts of methods of adding codes to posts to specify where the content of their plugin should be included. Shortcodes takes this idea, and builds a standard method into WordPress.

There is one included in WordPress 2.5 by default and that is the gallery shortcode. The gallery shortcode inserts all of the images that are attached to the post into that post using one simple code that looks like this: [gallery]

Shortcodes also accept options so, for example, if you wanted to include the gallery from a different post you can specify the post id, like this: [gallery id=”6″]

The gallery shortcode also lets you specify the order of the images, the size, the way they are marked up, by default it uses a definition list, and the number of columns. You can read more about the options in the gallery shortcode at the WordPress Codex.

Shortcodes can even be used to surround text, if the plugin requires it: [code]Some text here[/code]

Shortcodes should help to bring consistency to the way plugins work, helping users to get to grips with them more easily, but also help plugin authors. There is an easy to use API that takes the work out of producing plugins that find and replace content, or even just insert it, with options.

I have used it for one plugin so far. Fun with Photo Data displays EXIF data in a list. While it wasn’t difficult to create without shortcodes, using the Shorcodes API made it easier still. For complex plugins this could be make a significant difference.

My WordPress plugin generator has been updated to include shortcode functionality and you can read more information about the Shortcodes API at the WordPress Codex.

 

Comments

Leave a Reply

I am currently testing a comment link policy which means commenters do not get a link. There is a poll, and open comments for feedback on the comment policy page.

Please note. I am currently using an experimental antispam technique on this blog. If you run into problems please let me know using the Get in Touch link at the top of the page. Thanks, Andy.

Subscribe without commenting

Feed Icon - Get fed with RSS