Inheritances
Files
Overview
FRAMES
NO FRAMES

File shortcodes.php

WordPress API for creating bbcode like tags or what WordPress calls
"shortcodes." The tag and attribute parsing or regular expression code is
based on the Textpattern tag parser.

A few examples are below:

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

Shortcode tags support attributes and enclosed content, but does not entirely
support inline shortcodes in other shortcodes. You will have to call the
shortcode parser in your function to account for that.

{@internal
Please be aware that the above note was made during the beta of WordPress 2.6
and in the future may not be accurate. Please update the note when it is no
longer the case.}}

To apply shortcode tags to content:


$out = do_shortcode($content);
Linkhttp://codex.wordpress.org/Shortcode_API
PackageWordPress
SubpackageShortcodes
Since2.5

Summary

Global variables
$shortcode_tags Container for storing shortcode tags and their hook to call for the shortcode
Functions
add_shortcode() Add hook for shortcode tag.
do_shortcode() Search content for shortcodes and filter shortcodes through their hooks.
do_shortcode_tag() Regular Expression callable for do_shortcode() for calling shortcode hook
get_shortcode_regex() Retrieve the shortcode regular expression for searching.
remove_all_shortcodes() Clear all shortcodes.
remove_shortcode() Removes hook for shortcode
shortcode_atts() Combine user attributes with known attributes and fill in defaults when needed.
shortcode_parse_atts() Retrieve all attributes from the shortcodes tag.
strip_shortcodes() Remove all shortcode tags from the given content

Details

Global variables

$shortcode_tags
$shortcode_tags = array()
Container for storing shortcode tags and their hook to call for the shortcode
Since 2.5
Name $shortcode_tags
Var array
Global array $shortcode_tags

Functions

add_shortcode()
function add_shortcode( $tag
$func
)
Add hook for shortcode tag.

There can only be one hook for each shortcode. Which means that if another
plugin has a similar shortcode, it will override yours or yours will override
theirs depending on which order the plugins are included and/or ran.

Simplest example of a shortcode tag using the API:


// [footag foo="bar"]
function footag_func($atts) {
return "foo = {$atts[foo]}";
}
add_shortcode('footag', 'footag_func');


Example with nice attribute defaults:


// [bartag foo="bar"]
function bartag_func($atts) {
extract(shortcode_atts(array(
'foo' => 'no foo',
'baz' => 'default baz',
), $atts));

return "foo = {$foo}";
}
add_shortcode('bartag', 'bartag_func');


Example with enclosed content:


// [baztag]content[/baztag]
function baztag_func($atts, $content='') {
return "content = $content";
}
add_shortcode('baztag', 'baztag_func');
Parameters
string $tag Shortcode tag to be searched in post content.
callable $func Hook to run when shortcode is found.
Since 2.5
Uses $shortcode_tags
do_shortcode()
function do_shortcode( $content)
Search content for shortcodes and filter shortcodes through their hooks.

If there are no shortcode tags defined, then the content will be returned
without any filtering. This might cause issues when plugins are disabled but
the shortcode will still show up in the post or content.
Parameters
string $content Content to search for shortcodes
Returns string Content with shortcodes filtered out.
Since 2.5
Uses $shortcode_tags
Uses get_shortcode_regex() Gets the search pattern for searching shortcodes.
do_shortcode_tag()
function do_shortcode_tag( $m)
Regular Expression callable for do_shortcode() for calling shortcode hook.
Parameters
array $m Regular expression match array
Returns mixed False on failure.
See also get_shortcode_regex for details of the match array contents.
Since 2.5
Access private
Uses $shortcode_tags
get_shortcode_regex()
function get_shortcode_regex( )
Retrieve the shortcode regular expression for searching.

The regular expression combines the shortcode tags in the regular expression
in a regex class.

The regular expresion contains 6 different sub matches to help with parsing.

1/6 - An extra [ or ] to allow for escaping shortcodes with double [[]]
2 - The shortcode name
3 - The shortcode argument list
4 - The self closing /
5 - The content of a shortcode when it wraps some content.
Returns string The shortcode search regular expression
Since 2.5
Uses $shortcode_tags
remove_all_shortcodes()
function remove_all_shortcodes( )
Clear all shortcodes.

This function is simple, it clears all of the shortcode tags by replacing the
shortcodes global by a empty array. This is actually a very efficient method
for removing all shortcodes.
Since 2.5
Uses $shortcode_tags
remove_shortcode()
function remove_shortcode( $tag)
Removes hook for shortcode.
Parameters
string $tag shortcode tag to remove hook for.
Since 2.5
Uses $shortcode_tags
shortcode_atts()
function shortcode_atts( $pairs
$atts
)
Combine user attributes with known attributes and fill in defaults when needed.

The pairs should be considered to be all of the attributes which are
supported by the caller and given as a list. The returned attributes will
only contain the attributes in the $pairs list.

If the $atts list has unsupported attributes, then they will be ignored and
removed from the final returned list.
Parameters
array $pairs Entire list of supported attributes and their defaults.
array $atts User defined attributes in shortcode tag.
Returns array Combined and filtered attribute list.
Since 2.5
shortcode_parse_atts()
function shortcode_parse_atts( $text)
Retrieve all attributes from the shortcodes tag.

The attributes list has the attribute name as the key and the value of the
attribute as the value in the key/value pair. This allows for easier
retrieval of the attributes, since all attributes have to be known.
Parameters
string $text
Returns array List of attributes and their value.
Since 2.5
strip_shortcodes()
function strip_shortcodes( $content)
Remove all shortcode tags from the given content.
Parameters
string $content Content to remove shortcode tags.
Returns string Content without shortcode tags.
Since 2.5
Uses $shortcode_tags