WordPress.org

Ready to get started?Download WordPress

Codex

Shortcode

Since Version 2.5 WordPress support so called Shortcodes. They have been introduced for creating macros to be use in a post's content. For examples of shortcodes and how to use them, see WordPress.com Shortcodes, though some shortcodes featured there are exclusive to WordPress.com.

A trivial shortcode for a gallery looks like this:

[gallery]

Shortcodes can also be used with additional attributes as the following example shows:

[gallery id="123" size="medium"]

Both examples will display an image gallery, which would be hard to maintain when writing the HTML markup for it and keeping it in sync with uploaded images. By using a shortcode, the markup for the gallery will be created dynamically and automatically.

A shortcode will not operate in the sidebar without the proper filter in place.

add_filter( 'widget_text', 'shortcode_unautop');
add_filter('widget_text', 'do_shortcode');

The second line is the one that makes the shortcodes work, but you’ll want to include both. WordPress will apply the autop filter — the one that turns your line breaks into paragraph and break tags. The first line prevents that from happening.


add_filter( 'widget_text', 'shortcode_unautop'); add_filter( 'widget_text', 'do_shortcode');


Built-In and Additional Shortcodes

WordPress does offer some of the shortcodes by default (for example the gallery one) and plugins can add their own as well via the Shortcode API.

The following shortcodes are included with WordPress:

Escaping Shortcodes

Sometimes you may wish to use the text which usually represents a shortcode in your posts. To do this, you need to escape the shortcode by using two sets of brackets instead of just one. So to display thisin your post:

[gallery]

You would write this:

[[gallery]]

For shortcodes that have a start and end tag, you only need to escape the very first and very last brackets:

[[shortcode] ... [/shortcode]]

Note that only shortcodes that are currently available can be escaped in this manner. Using double brackets around text that is not currently registered as a shortcode will result in both sets of brackets being displayed in your post, not just one as when escaping registered shortcodes.