3 * WordPress API for creating bbcode like tags or what WordPress calls
4 * "shortcodes." The tag and attribute parsing or regular expression code is
5 * based on the Textpattern tag parser.
7 * A few examples are below:
10 * [shortcode foo="bar" baz="bing" /]
11 * [shortcode foo="bar"]content[/shortcode]
13 * Shortcode tags support attributes and enclosed content, but does not entirely
14 * support inline shortcodes in other shortcodes. You will have to call the
15 * shortcode parser in your function to account for that.
18 * Please be aware that the above note was made during the beta of WordPress 2.6
19 * and in the future may not be accurate. Please update the note when it is no
22 * To apply shortcode tags to content:
25 * $out = do_shortcode($content);
28 * @link http://codex.wordpress.org/Shortcode_API
31 * @subpackage Shortcodes
36 * Container for storing shortcode tags and their hook to call for the shortcode
39 * @name $shortcode_tags
41 * @global array $shortcode_tags
43 $shortcode_tags = array();
46 * Add hook for shortcode tag.
48 * There can only be one hook for each shortcode. Which means that if another
49 * plugin has a similar shortcode, it will override yours or yours will override
50 * theirs depending on which order the plugins are included and/or ran.
52 * Simplest example of a shortcode tag using the API:
55 * // [footag foo="bar"]
56 * function footag_func($atts) {
57 * return "foo = {$atts[foo]}";
59 * add_shortcode('footag', 'footag_func');
62 * Example with nice attribute defaults:
65 * // [bartag foo="bar"]
66 * function bartag_func($atts) {
67 * extract(shortcode_atts(array(
69 * 'baz' => 'default baz',
72 * return "foo = {$foo}";
74 * add_shortcode('bartag', 'bartag_func');
77 * Example with enclosed content:
80 * // [baztag]content[/baztag]
81 * function baztag_func($atts, $content='') {
82 * return "content = $content";
84 * add_shortcode('baztag', 'baztag_func');
88 * @uses $shortcode_tags
90 * @param string $tag Shortcode tag to be searched in post content.
91 * @param callable $func Hook to run when shortcode is found.
93 function add_shortcode($tag, $func) {
94 global $shortcode_tags;
96 if ( is_callable($func) )
97 $shortcode_tags[$tag] = $func;
101 * Removes hook for shortcode.
104 * @uses $shortcode_tags
106 * @param string $tag shortcode tag to remove hook for.
108 function remove_shortcode($tag) {
109 global $shortcode_tags;
111 unset($shortcode_tags[$tag]);
115 * Clear all shortcodes.
117 * This function is simple, it clears all of the shortcode tags by replacing the
118 * shortcodes global by a empty array. This is actually a very efficient method
119 * for removing all shortcodes.
122 * @uses $shortcode_tags
124 function remove_all_shortcodes() {
125 global $shortcode_tags;
127 $shortcode_tags = array();
131 * Search content for shortcodes and filter shortcodes through their hooks.
133 * If there are no shortcode tags defined, then the content will be returned
134 * without any filtering. This might cause issues when plugins are disabled but
135 * the shortcode will still show up in the post or content.
138 * @uses $shortcode_tags
139 * @uses get_shortcode_regex() Gets the search pattern for searching shortcodes.
141 * @param string $content Content to search for shortcodes
142 * @return string Content with shortcodes filtered out.
144 function do_shortcode($content) {
145 global $shortcode_tags;
147 if (empty($shortcode_tags) || !is_array($shortcode_tags))
150 $pattern = get_shortcode_regex();
151 return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
155 * Retrieve the shortcode regular expression for searching.
157 * The regular expression combines the shortcode tags in the regular expression
160 * The regular expression contains 6 different sub matches to help with parsing.
162 * 1 - An extra [ to allow for escaping shortcodes with double [[]]
163 * 2 - The shortcode name
164 * 3 - The shortcode argument list
165 * 4 - The self closing /
166 * 5 - The content of a shortcode when it wraps some content.
167 * 6 - An extra ] to allow for escaping shortcodes with double [[]]
170 * @uses $shortcode_tags
172 * @return string The shortcode search regular expression
174 function get_shortcode_regex() {
175 global $shortcode_tags;
176 $tagnames = array_keys($shortcode_tags);
177 $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
179 // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
180 // Also, see shortcode_unautop() and shortcode.js.
182 '\\[' // Opening bracket
183 . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
184 . "($tagregexp)" // 2: Shortcode name
185 . '(?![\\w-])' // Not followed by word character or hyphen
186 . '(' // 3: Unroll the loop: Inside the opening shortcode tag
187 . '[^\\]\\/]*' // Not a closing bracket or forward slash
189 . '\\/(?!\\])' // A forward slash not followed by a closing bracket
190 . '[^\\]\\/]*' // Not a closing bracket or forward slash
194 . '(\\/)' // 4: Self closing tag ...
195 . '\\]' // ... and closing bracket
197 . '\\]' // Closing bracket
199 . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
200 . '[^\\[]*+' // Not an opening bracket
202 . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
203 . '[^\\[]*+' // Not an opening bracket
206 . '\\[\\/\\2\\]' // Closing shortcode tag
209 . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
213 * Regular Expression callable for do_shortcode() for calling shortcode hook.
214 * @see get_shortcode_regex for details of the match array contents.
218 * @uses $shortcode_tags
220 * @param array $m Regular expression match array
221 * @return mixed False on failure.
223 function do_shortcode_tag( $m ) {
224 global $shortcode_tags;
226 // allow [[foo]] syntax for escaping a tag
227 if ( $m[1] == '[' && $m[6] == ']' ) {
228 return substr($m[0], 1, -1);
232 $attr = shortcode_parse_atts( $m[3] );
234 if ( isset( $m[5] ) ) {
235 // enclosing tag - extra parameter
236 return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6];
239 return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, null, $tag ) . $m[6];
244 * Retrieve all attributes from the shortcodes tag.
246 * The attributes list has the attribute name as the key and the value of the
247 * attribute as the value in the key/value pair. This allows for easier
248 * retrieval of the attributes, since all attributes have to be known.
252 * @param string $text
253 * @return array List of attributes and their value.
255 function shortcode_parse_atts($text) {
257 $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
258 $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
259 if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
260 foreach ($match as $m) {
262 $atts[strtolower($m[1])] = stripcslashes($m[2]);
263 elseif (!empty($m[3]))
264 $atts[strtolower($m[3])] = stripcslashes($m[4]);
265 elseif (!empty($m[5]))
266 $atts[strtolower($m[5])] = stripcslashes($m[6]);
267 elseif (isset($m[7]) and strlen($m[7]))
268 $atts[] = stripcslashes($m[7]);
269 elseif (isset($m[8]))
270 $atts[] = stripcslashes($m[8]);
273 $atts = ltrim($text);
279 * Combine user attributes with known attributes and fill in defaults when needed.
281 * The pairs should be considered to be all of the attributes which are
282 * supported by the caller and given as a list. The returned attributes will
283 * only contain the attributes in the $pairs list.
285 * If the $atts list has unsupported attributes, then they will be ignored and
286 * removed from the final returned list.
290 * @param array $pairs Entire list of supported attributes and their defaults.
291 * @param array $atts User defined attributes in shortcode tag.
292 * @return array Combined and filtered attribute list.
294 function shortcode_atts($pairs, $atts) {
295 $atts = (array)$atts;
297 foreach($pairs as $name => $default) {
298 if ( array_key_exists($name, $atts) )
299 $out[$name] = $atts[$name];
301 $out[$name] = $default;
307 * Remove all shortcode tags from the given content.
310 * @uses $shortcode_tags
312 * @param string $content Content to remove shortcode tags.
313 * @return string Content without shortcode tags.
315 function strip_shortcodes( $content ) {
316 global $shortcode_tags;
318 if (empty($shortcode_tags) || !is_array($shortcode_tags))
321 $pattern = get_shortcode_regex();
323 return preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
326 function strip_shortcode_tag( $m ) {
327 // allow [[foo]] syntax for escaping a tag
328 if ( $m[1] == '[' && $m[6] == ']' ) {
329 return substr($m[0], 1, -1);
332 return $m[1] . $m[6];
335 add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()