WordPress Shortcode API

WordPress 2.5 extended the tagging function and added many little features. Among others the shortcode API for example, where you can add strings to a content in a fast and easy way. This makes the call from other functions in Plugins or theme functions much more easier.

The possibilities of shortcodes are immense, many other applications love this feature and with the new one you can make it even easier for the users to write in their backend. To put Shortcodes into the quick tag menu, I recommend to use the AddQuicktag Plugin.

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.

The possibilities

[shortcode ]
[shortcode /]
[shortcode foo="bar" /]
[shortcode foo="bar" baz="bing" /]
[shortcode ]content[shortcode /]
[shortcode foo="bar"]content[/shortcode]

Explanation

For a little overview, here is a brief explanation how it works. Responsible is the function add_shortcode(). You can find more in /wp-includes/shortcodes.php, including a „short“ documentation.

The following example, if used in a Plugin or theme, will take care that the syntax [tag1] assigned in the content will be replaced by the text Some Longer Text in the front end of your blog.

// [tag1] --> Some Longer Text
function shortcode_example() {

	return 'Some Longer Text';
}

add_shortcode('tag1', 'shortcode_example');

The screenshot explains it a little bit better.

Screenshot of an example to the shortcode in WordPress
Screenshot of an example to the shortcode in WordPress


Posted

in

by

Comments

7 responses to “WordPress Shortcode API”

  1. Andy Bailey Avatar

    that is going to be very useful for a plugin I am writing to show flickr pictures in a post for keywords. much easier than making the user install exec-php to add a plugin call to a post content!

    I’m lovin all the tips on this site especially for wp2.7

    do you have a twitter id?

  2. Frank Avatar

    We are 3 people, my Twitter ID is bueltge and you can also see WP Engineer News on this ID.

  3. Chris Martin Avatar

    Very nice explanation and simple example. I’ve been searching all over looking for a solution to adding my code in posts via tag. 2 minutes and a few changes to your code and it’s up and running. Nice work..

    Global Dream Renewable Energy

  4. Alex Avatar

    Hey Chris, glad to hear that 馃檪

  5. […] Wi臋cej przyk艂ad贸w i informacji dotycz膮cych kr贸tkich kod贸w: Oficjalne API WordPress Smashing Magazine WPEngineer […]

  6. Jatinder Avatar

    Excellent post on how to add a custom shortcode. Precise and to the point.

    Thank You!