In: , , ,
On: 2008 / 03 / 28 Viewed: 56559 times
Shorter URL for this post: http://ozh.in/hb

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

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/hb

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.

12 Blablas

    Pages: [2] 1 » Show All

  1. 12
    Kristina Canada »
    thought, on 24/Aug/10 at 5:17 pm # :

    Ahh,, figured it out. Works fine,,, except

    My short-code shows in the xml feed of the site as short-code. How would I apply the filter to prevent this from happening?

  2. 11
    Kris Canada »
    wrote, on 23/Aug/10 at 9:15 am # :

    Sorry,,

    My output is still

    [tag3]Hello[/tag3]

    not [tag3]something[/tag3]

Pages: [2] 1 » Show All

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