In: , , ,
On: 2008 / 03 / 28 Viewed: 16026 times

WordPress 2.5 comes with a very handy API that plugin authors should love: shortcodes, ie. BBcode-like tags, that are parsed when found in post contents. The API is very easy to use, and well done enough that it's relatively foolproof.

Shortcode types

The API allows for all the possible combinations and forms you'd think of: selfclosing or open tags, attributes with single or double quotes.

CODE:
  1. [tag]
  2. [tag /]
  3. [tag attribute="value" /]
  4. [tag attr1='value1' attr2="value2" ]
  5. [tag]enclosed content[/tag]
  6. [tag attr="value"]enclosed content[/tag]

The Concept

First, you have to define a function that will take care of your shortcodes, their potential attributes, and any enclosed content:

PHP:
  1. function my_shortcode_func($attr, $content) {
  2.     // $attr is an array of $key=>$value pairs
  3.     // $content is a string containing the enclosed content
  4.     // ... do something with $attr and $content
  5.     // return the expected result
  6. }

Then, to register your new shortcode so that [tag attr="value"]content[/tag] is processed as wished by my_shortcode_func(), you just add:

PHP:
  1. add_shortcode('tag', 'my_shortcode_func');

All API functions

The API comes with a set of mostly self explanatory functions you can use:

PHP:
  1. add_shortcode('tag', 'function_name'); // register a new shortcode
  2. remove_shortcode('tag'); // unregister
  3. remove_all_shortcodes(); // just as it says
  4. $return = do_shortcode($content); // apply shortcodes to content without echoing anything

Examples

[tag1] → Some Longer Text

PHP:
  1. function shortcode_example1() {
  2.     return 'Some Longer Text';
  3. }
  4. add_shortcode('tag1', 'shortcode_example1');

[tag2 param="something"] → Some Longer Text and something

PHP:
  1. function shortcode_example2($attr) {
  2.     return 'Some Longer Text and '.$attr['param'];
  3. }
  4. add_shortcode('tag2', 'shortcode_example2');

[tag3]something[/tag3] → You just said "something", didn't you ?

PHP:
  1. function shortcode_example3($attr, $content) {
  2.     return "You just said '$content', didn't you ?";
  3. }
  4. add_shortcode('tag3', 'shortcode_example3');

[tag4 any attribute]something[/tag4] → Attributes: {list of attributes}. Content: {content}

PHP:
  1. function shortcode_example4($attr, $content) {
  2.     $attributes = '';
  3.     foreach ($attr as $key=>$value) {
  4.         $attributes .= "$key: $value\n";
  5.     }
  6.     return "Attributes: $attributes<br/>Content: $content";
  7. }
  8. add_shortcode('tag4', 'shortcode_example4');

Experiment and read more

Test and experiment: all the examples above are gathered into this simple Shortcodes plugin example (rename as shortcodes.php and put in your plugin directory). It's interesting to try to break things with unexpected attributes or enclosed content: the API is pretty foolproof and should ignore things just as you'd thought it should.

Read more : the whole API can be found in wp-includes/shortcodes.php and is relatively well commented. Jacob Santos also has a nice and detailed overview of the API

Related posts

Metastuff

This entry "WordPress 2.5 ShortCodes API Overview" was posted on 28/03/2008 at 12:35 pm and is tagged with , , ,
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site

4 Blablas

  1. 1
    johnbillion Great Britain (UK) »
    thought, on 28/Mar/08 at 6:20 pm # :

    Ozh, I'm really loving your introductions to the new APIs in WordPress. Great examples and great writing. Thanks and keep it up!

  2. 2
    Paul Menard United States »
    wrote, on 12/May/08 at 1:07 am # :

    So one thing I've wondered about with the new shortcode. Since this is created in the actual post content what safe guards are in place to prevent the unfiltered shortcode from being passed the the presentation layer or viewable user version?

    This was my biggest issue with some of the earlier shortcode plugin for like SWF or youtube videos. I add the code and link to the video into my post. Then if later I deactivated the plugin or changed plugins the shortcode would be revealed to the user.

  3. 3
    Ozh France »
    commented, on 12/May/08 at 1:19 am # :

    Paul Menard » I had the same concern and proposed that instead/in parallel of [stuff] it should be <!--stuff--> but despite some traction the idea was rejected...
    So basically, yes, when you disable a plugin that parses shortcodes, everybody see them. Quite lame.

  4. 4
    WordPress 2.5 Shortcode API - bueltge.de... Germany »
    pingback on 28/May/08 at 7:18 am # :

    [...] kleine Einführung und ersichtliche Erfolge gibt es in Kurzversion in den folgenden Zeilen. Wie es genau geht und ein Plugin mit verschiedenen Beispielen gibt es bei Ozh, der derzeit auch die neue WordPress Funktion [...]

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

Close
E-mail It