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:
24 * $out = do_shortcode( $content );
26 * @link https://codex.wordpress.org/Shortcode_API
29 * @subpackage Shortcodes
34 * Container for storing shortcode tags and their hook to call for the shortcode
38 * @name $shortcode_tags
40 * @global array $shortcode_tags
42 $shortcode_tags = array();
45 * Add hook for shortcode tag.
47 * There can only be one hook for each shortcode. Which means that if another
48 * plugin has a similar shortcode, it will override yours or yours will override
49 * theirs depending on which order the plugins are included and/or ran.
51 * Simplest example of a shortcode tag using the API:
53 * // [footag foo="bar"]
54 * function footag_func( $atts ) {
59 * add_shortcode( 'footag', 'footag_func' );
61 * Example with nice attribute defaults:
63 * // [bartag foo="bar"]
64 * function bartag_func( $atts ) {
65 * $args = shortcode_atts( array(
67 * 'baz' => 'default baz',
70 * return "foo = {$args['foo']}";
72 * add_shortcode( 'bartag', 'bartag_func' );
74 * Example with enclosed content:
76 * // [baztag]content[/baztag]
77 * function baztag_func( $atts, $content = '' ) {
78 * return "content = $content";
80 * add_shortcode( 'baztag', 'baztag_func' );
84 * @uses $shortcode_tags
86 * @param string $tag Shortcode tag to be searched in post content.
87 * @param callable $func Hook to run when shortcode is found.
89 function add_shortcode($tag, $func) {
90 global $shortcode_tags;
92 if ( is_callable($func) )
93 $shortcode_tags[$tag] = $func;
97 * Removes hook for shortcode.
101 * @uses $shortcode_tags
103 * @param string $tag Shortcode tag to remove hook for.
105 function remove_shortcode($tag) {
106 global $shortcode_tags;
108 unset($shortcode_tags[$tag]);
112 * Clear all shortcodes.
114 * This function is simple, it clears all of the shortcode tags by replacing the
115 * shortcodes global by a empty array. This is actually a very efficient method
116 * for removing all shortcodes.
120 * @uses $shortcode_tags
122 function remove_all_shortcodes() {
123 global $shortcode_tags;
125 $shortcode_tags = array();
129 * Whether a registered shortcode exists named $tag
133 * @global array $shortcode_tags List of shortcode tags and their callback hooks.
135 * @param string $tag Shortcode tag to check.
136 * @return bool Whether the given shortcode exists.
138 function shortcode_exists( $tag ) {
139 global $shortcode_tags;
140 return array_key_exists( $tag, $shortcode_tags );
144 * Whether the passed content contains the specified shortcode
148 * @global array $shortcode_tags
150 * @param string $content Content to search for shortcodes.
151 * @param string $tag Shortcode tag to check.
152 * @return bool Whether the passed content contains the given shortcode.
154 function has_shortcode( $content, $tag ) {
155 if ( false === strpos( $content, '[' ) ) {
159 if ( shortcode_exists( $tag ) ) {
160 preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
161 if ( empty( $matches ) )
164 foreach ( $matches as $shortcode ) {
165 if ( $tag === $shortcode[2] ) {
167 } elseif ( ! empty( $shortcode[5] ) && has_shortcode( $shortcode[5], $tag ) ) {
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 * @global array $shortcode_tags List of shortcode tags and their callback hooks.
186 * @param string $content Content to search for shortcodes.
187 * @return string Content with shortcodes filtered out.
189 function do_shortcode($content) {
190 global $shortcode_tags;
192 if ( false === strpos( $content, '[' ) ) {
196 if (empty($shortcode_tags) || !is_array($shortcode_tags))
199 $pattern = get_shortcode_regex();
200 return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
204 * Retrieve the shortcode regular expression for searching.
206 * The regular expression combines the shortcode tags in the regular expression
209 * The regular expression contains 6 different sub matches to help with parsing.
211 * 1 - An extra [ to allow for escaping shortcodes with double [[]]
212 * 2 - The shortcode name
213 * 3 - The shortcode argument list
214 * 4 - The self closing /
215 * 5 - The content of a shortcode when it wraps some content.
216 * 6 - An extra ] to allow for escaping shortcodes with double [[]]
220 * @uses $shortcode_tags
222 * @return string The shortcode search regular expression
224 function get_shortcode_regex() {
225 global $shortcode_tags;
226 $tagnames = array_keys($shortcode_tags);
227 $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
229 // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
230 // Also, see shortcode_unautop() and shortcode.js.
232 '\\[' // Opening bracket
233 . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
234 . "($tagregexp)" // 2: Shortcode name
235 . '(?![\\w-])' // Not followed by word character or hyphen
236 . '(' // 3: Unroll the loop: Inside the opening shortcode tag
237 . '[^\\]\\/]*' // Not a closing bracket or forward slash
239 . '\\/(?!\\])' // A forward slash not followed by a closing bracket
240 . '[^\\]\\/]*' // Not a closing bracket or forward slash
244 . '(\\/)' // 4: Self closing tag ...
245 . '\\]' // ... and closing bracket
247 . '\\]' // Closing bracket
249 . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
250 . '[^\\[]*+' // Not an opening bracket
252 . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
253 . '[^\\[]*+' // Not an opening bracket
256 . '\\[\\/\\2\\]' // Closing shortcode tag
259 . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
263 * Regular Expression callable for do_shortcode() for calling shortcode hook.
264 * @see get_shortcode_regex for details of the match array contents.
268 * @uses $shortcode_tags
270 * @param array $m Regular expression match array
271 * @return mixed False on failure.
273 function do_shortcode_tag( $m ) {
274 global $shortcode_tags;
276 // allow [[foo]] syntax for escaping a tag
277 if ( $m[1] == '[' && $m[6] == ']' ) {
278 return substr($m[0], 1, -1);
282 $attr = shortcode_parse_atts( $m[3] );
284 if ( isset( $m[5] ) ) {
285 // enclosing tag - extra parameter
286 return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, $m[5], $tag ) . $m[6];
289 return $m[1] . call_user_func( $shortcode_tags[$tag], $attr, null, $tag ) . $m[6];
294 * Retrieve all attributes from the shortcodes tag.
296 * The attributes list has the attribute name as the key and the value of the
297 * attribute as the value in the key/value pair. This allows for easier
298 * retrieval of the attributes, since all attributes have to be known.
302 * @param string $text
303 * @return array List of attributes and their value.
305 function shortcode_parse_atts($text) {
307 $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
308 $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
309 if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
310 foreach ($match as $m) {
312 $atts[strtolower($m[1])] = stripcslashes($m[2]);
313 elseif (!empty($m[3]))
314 $atts[strtolower($m[3])] = stripcslashes($m[4]);
315 elseif (!empty($m[5]))
316 $atts[strtolower($m[5])] = stripcslashes($m[6]);
317 elseif (isset($m[7]) && strlen($m[7]))
318 $atts[] = stripcslashes($m[7]);
319 elseif (isset($m[8]))
320 $atts[] = stripcslashes($m[8]);
323 $atts = ltrim($text);
329 * Combine user attributes with known attributes and fill in defaults when needed.
331 * The pairs should be considered to be all of the attributes which are
332 * supported by the caller and given as a list. The returned attributes will
333 * only contain the attributes in the $pairs list.
335 * If the $atts list has unsupported attributes, then they will be ignored and
336 * removed from the final returned list.
340 * @param array $pairs Entire list of supported attributes and their defaults.
341 * @param array $atts User defined attributes in shortcode tag.
342 * @param string $shortcode Optional. The name of the shortcode, provided for context to enable filtering
343 * @return array Combined and filtered attribute list.
345 function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
346 $atts = (array)$atts;
348 foreach($pairs as $name => $default) {
349 if ( array_key_exists($name, $atts) )
350 $out[$name] = $atts[$name];
352 $out[$name] = $default;
355 * Filter a shortcode's default attributes.
357 * If the third parameter of the shortcode_atts() function is present then this filter is available.
358 * The third parameter, $shortcode, is the name of the shortcode.
362 * @param array $out The output array of shortcode attributes.
363 * @param array $pairs The supported attributes and their defaults.
364 * @param array $atts The user defined shortcode attributes.
367 $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts );
373 * Remove all shortcode tags from the given content.
377 * @uses $shortcode_tags
379 * @param string $content Content to remove shortcode tags.
380 * @return string Content without shortcode tags.
382 function strip_shortcodes( $content ) {
383 global $shortcode_tags;
385 if ( false === strpos( $content, '[' ) ) {
389 if (empty($shortcode_tags) || !is_array($shortcode_tags))
392 $pattern = get_shortcode_regex();
394 return preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
397 function strip_shortcode_tag( $m ) {
398 // allow [[foo]] syntax for escaping a tag
399 if ( $m[1] == '[' && $m[6] == ']' ) {
400 return substr($m[0], 1, -1);
403 return $m[1] . $m[6];