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
40 * @name $shortcode_tags
42 * @global array $shortcode_tags
44 $shortcode_tags = array();
47 * Add hook for shortcode tag.
49 * There can only be one hook for each shortcode. Which means that if another
50 * plugin has a similar shortcode, it will override yours or yours will override
51 * theirs depending on which order the plugins are included and/or ran.
53 * Simplest example of a shortcode tag using the API:
56 * // [footag foo="bar"]
57 * function footag_func($atts) {
58 * return "foo = {$atts[foo]}";
60 * add_shortcode('footag', 'footag_func');
63 * Example with nice attribute defaults:
66 * // [bartag foo="bar"]
67 * function bartag_func($atts) {
68 * extract(shortcode_atts(array(
70 * 'baz' => 'default baz',
73 * return "foo = {$foo}";
75 * add_shortcode('bartag', 'bartag_func');
78 * Example with enclosed content:
81 * // [baztag]content[/baztag]
82 * function baztag_func($atts, $content='') {
83 * return "content = $content";
85 * add_shortcode('baztag', 'baztag_func');
90 * @uses $shortcode_tags
92 * @param string $tag Shortcode tag to be searched in post content.
93 * @param callable $func Hook to run when shortcode is found.
95 function add_shortcode($tag, $func) {
96 global $shortcode_tags;
98 if ( is_callable($func) )
99 $shortcode_tags[$tag] = $func;
103 * Removes hook for shortcode.
107 * @uses $shortcode_tags
109 * @param string $tag shortcode tag to remove hook for.
111 function remove_shortcode($tag) {
112 global $shortcode_tags;
114 unset($shortcode_tags[$tag]);
118 * Clear all shortcodes.
120 * This function is simple, it clears all of the shortcode tags by replacing the
121 * shortcodes global by a empty array. This is actually a very efficient method
122 * for removing all shortcodes.
126 * @uses $shortcode_tags
128 function remove_all_shortcodes() {
129 global $shortcode_tags;
131 $shortcode_tags = array();
135 * Whether a registered shortcode exists named $tag
139 * @global array $shortcode_tags
143 function shortcode_exists( $tag ) {
144 global $shortcode_tags;
145 return array_key_exists( $tag, $shortcode_tags );
149 * Whether the passed content contains the specified shortcode
153 * @global array $shortcode_tags
157 function has_shortcode( $content, $tag ) {
158 if ( false === strpos( $content, '[' ) ) {
162 if ( shortcode_exists( $tag ) ) {
163 preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
164 if ( empty( $matches ) )
167 foreach ( $matches as $shortcode ) {
168 if ( $tag === $shortcode[2] )
176 * Search content for shortcodes and filter shortcodes through their hooks.
178 * If there are no shortcode tags defined, then the content will be returned
179 * without any filtering. This might cause issues when plugins are disabled but
180 * the shortcode will still show up in the post or content.
184 * @uses $shortcode_tags
185 * @uses get_shortcode_regex() Gets the search pattern for searching shortcodes.
187 * @param string $content Content to search for shortcodes
188 * @return string Content with shortcodes filtered out.
190 function do_shortcode($content) {
191 global $shortcode_tags;
193 if ( false === strpos( $content, '[' ) ) {
197 if (empty($shortcode_tags) || !is_array($shortcode_tags))
200 $pattern = get_shortcode_regex();
201 return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
205 * Retrieve the shortcode regular expression for searching.
207 * The regular expression combines the shortcode tags in the regular expression
210 * The regular expression contains 6 different sub matches to help with parsing.
212 * 1 - An extra [ to allow for escaping shortcodes with double [[]]
213 * 2 - The shortcode name
214 * 3 - The shortcode argument list
215 * 4 - The self closing /
216 * 5 - The content of a shortcode when it wraps some content.
217 * 6 - An extra ] to allow for escaping shortcodes with double [[]]
221 * @uses $shortcode_tags
223 * @return string The shortcode search regular expression
225 function get_shortcode_regex() {
226 global $shortcode_tags;
227 $tagnames = array_keys($shortcode_tags);
228 $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
230 // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
231 // Also, see shortcode_unautop() and shortcode.js.
233 '\\[' // Opening bracket
234 . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
235 . "($tagregexp)" // 2: Shortcode name
236 . '(?![\\w-])' // Not followed by word character or hyphen
237 . '(' // 3: Unroll the loop: Inside the opening shortcode tag
238 . '[^\\]\\/]*' // Not a closing bracket or forward slash
240 . '\\/(?!\\])' // A forward slash not followed by a closing bracket
241 . '[^\\]\\/]*' // Not a closing bracket or forward slash
245 . '(\\/)' // 4: Self closing tag ...
246 . '\\]' // ... and closing bracket
248 . '\\]' // Closing bracket
250 . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
251 . '[^\\[]*+' // Not an opening bracket
253 . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
254 . '[^\\[]*+' // Not an opening bracket
257 . '\\[\\/\\2\\]' // Closing shortcode tag
260 . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
264 * Regular Expression callable for do_shortcode() for calling shortcode hook.
265 * @see get_shortcode_regex for details of the match array contents.
269 * @uses $shortcode_tags
271 * @param array $m Regular expression match array
272 * @return mixed False on failure.
274 function do_shortcode_tag( $m ) {
275 global $shortcode_tags;
277 // allow [[foo]] syntax for escaping a tag
278 if ( $m[1] == '[' && $m[6] == ']' ) {
279 return substr($m[0], 1, -1);
283 $attr = shortcode_parse_atts( $m[3] );
285 if ( isset( $m[5] ) ) {
286 // enclosing tag - extra parameter
287 return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6];
290 return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, null, $tag ) . $m[6];
295 * Retrieve all attributes from the shortcodes tag.
297 * The attributes list has the attribute name as the key and the value of the
298 * attribute as the value in the key/value pair. This allows for easier
299 * retrieval of the attributes, since all attributes have to be known.
303 * @param string $text
304 * @return array List of attributes and their value.
306 function shortcode_parse_atts($text) {
308 $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
309 $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
310 if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
311 foreach ($match as $m) {
313 $atts[strtolower($m[1])] = stripcslashes($m[2]);
314 elseif (!empty($m[3]))
315 $atts[strtolower($m[3])] = stripcslashes($m[4]);
316 elseif (!empty($m[5]))
317 $atts[strtolower($m[5])] = stripcslashes($m[6]);
318 elseif (isset($m[7]) and strlen($m[7]))
319 $atts[] = stripcslashes($m[7]);
320 elseif (isset($m[8]))
321 $atts[] = stripcslashes($m[8]);
324 $atts = ltrim($text);
330 * Combine user attributes with known attributes and fill in defaults when needed.
332 * The pairs should be considered to be all of the attributes which are
333 * supported by the caller and given as a list. The returned attributes will
334 * only contain the attributes in the $pairs list.
336 * If the $atts list has unsupported attributes, then they will be ignored and
337 * removed from the final returned list.
341 * @param array $pairs Entire list of supported attributes and their defaults.
342 * @param array $atts User defined attributes in shortcode tag.
343 * @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering
344 * @return array Combined and filtered attribute list.
346 function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
347 $atts = (array)$atts;
349 foreach($pairs as $name => $default) {
350 if ( array_key_exists($name, $atts) )
351 $out[$name] = $atts[$name];
353 $out[$name] = $default;
356 * Filter a shortcode's default attributes.
358 * If the third parameter of the shortcode_atts() function is present then this filter is available.
359 * The third parameter, $shortcode, is the name of the shortcode.
363 * @param array $out The output array of shortcode attributes.
364 * @param array $pairs The supported attributes and their defaults.
365 * @param array $atts The user defined shortcode attributes.
368 $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts );
374 * Remove all shortcode tags from the given content.
378 * @uses $shortcode_tags
380 * @param string $content Content to remove shortcode tags.
381 * @return string Content without shortcode tags.
383 function strip_shortcodes( $content ) {
384 global $shortcode_tags;
386 if ( false === strpos( $content, '[' ) ) {
390 if (empty($shortcode_tags) || !is_array($shortcode_tags))
393 $pattern = get_shortcode_regex();
395 return preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
398 function strip_shortcode_tag( $m ) {
399 // allow [[foo]] syntax for escaping a tag
400 if ( $m[1] == '[' && $m[6] == ']' ) {
401 return substr($m[0], 1, -1);
404 return $m[1] . $m[6];
407 add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()