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.
-
[tag]
-
[tag /]
-
[tag attribute="value" /]
-
[tag attr1='value1' attr2="value2" ]
-
[tag]enclosed content[/tag]
-
[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:
-
function my_shortcode_func($attr, $content) {
-
// $attr is an array of $key=>$value pairs
-
// $content is a string containing the enclosed content
-
// ... do something with $attr and $content
-
// return the expected result
-
}
Then, to register your new shortcode so that [tag attr="value"]content[/tag] is processed as wished by my_shortcode_func(), you just add:
-
add_shortcode('tag', 'my_shortcode_func');
All API functions
The API comes with a set of mostly self explanatory functions you can use:
-
add_shortcode('tag', 'function_name'); // register a new shortcode
-
remove_shortcode('tag'); // unregister
-
remove_all_shortcodes(); // just as it says
-
$return = do_shortcode($content); // apply shortcodes to content without echoing anything
Examples
[tag1] → Some Longer Text
-
function shortcode_example1() {
-
return 'Some Longer Text';
-
}
-
add_shortcode('tag1', 'shortcode_example1');
[tag2 param="something"] → Some Longer Text and something
-
function shortcode_example2($attr) {
-
return 'Some Longer Text and '.$attr['param'];
-
}
-
add_shortcode('tag2', 'shortcode_example2');
[tag3]something[/tag3] → You just said "something", didn't you ?
-
function shortcode_example3($attr, $content) {
-
return "You just said '$content', didn't you ?";
-
}
-
add_shortcode('tag3', 'shortcode_example3');
[tag4 any attribute]something[/tag4] → Attributes: {list of attributes}. Content: {content}
-
function shortcode_example4($attr, $content) {
-
$attributes = '';
-
foreach ($attr as $key=>$value) {
-
$attributes .= "$key: $value\n";
-
}
-
return "Attributes: $attributes<br/>Content: $content";
-
}
-
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
Pages: [2] 1 » Show All
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?
wrote, on 23/Aug/10 at 9:15 am # :
Sorry,,
My output is still
[tag3]Hello[/tag3]
not [tag3]something[/tag3]