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
Shorter URL
Want to share or tweet this post? Please use this short URL: http://ozh.in/hb
Ozh, I'm really loving your introductions to the new APIs in WordPress. Great examples and great writing. Thanks and keep it up!
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.
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.
[…] 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 […]
[…] It's easy to understand and to use. I will give a short introduction with some results below. The functionality and all details behind, plus a plugin with different examples are available at Ozh. […]
I am trying to use short code to display the post id inside a post, however when I use [id] it is showing the post ID at the start of the post of the post content, not where I have typed it.
The shortcode I am using, I have placed this in the plugin folder.
What displays on the post page: 10741This is a good post. This is Post ID:
I also tried:
but it doesn't work either.
Jasmine » Your shortcode function *echoes* something when it's supposed to *return* content.
Hi Ozh i have the same problem with Jasmine and my shortcode returns the code part as always, i mean my shortcode is [ gpc id= "name" ] and on the top of all my pages/posts that i used shortcode name variable is seen.
in the function tried that if user enter "name" as shortcode show a div that named as "name".. How can i stop that? Thanks right now..
Problem is solved, i'am sorry for excess post. You all right, i've forgotten an echo command for testing.. Thanks for that nice writing..
So,,
I created a basic plugin and inserted this into it
// [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');
In the post I inserted this:
[tag3]Hello[/tag3]
and my output in the post is still
[tag3]something[/tag3]
Sorry,,
My output is still
[tag3]Hello[/tag3]
not [tag3]something[/tag3]
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?