mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * WordPress API for creating bbcode like tags or what WordPress calls
mp-wp_genesis 4 * "shortcodes." The tag and attribute parsing or regular expression code is
mp-wp_genesis 5 * based on the Textpattern tag parser.
mp-wp_genesis 6 *
mp-wp_genesis 7 * A few examples are below:
mp-wp_genesis 8 *
mp-wp_genesis 9 * [shortcode /]
mp-wp_genesis 10 * [shortcode foo="bar" baz="bing" /]
mp-wp_genesis 11 * [shortcode foo="bar"]content[/shortcode]
mp-wp_genesis 12 *
mp-wp_genesis 13 * Shortcode tags support attributes and enclosed content, but does not entirely
mp-wp_genesis 14 * support inline shortcodes in other shortcodes. You will have to call the
mp-wp_genesis 15 * shortcode parser in your function to account for that.
mp-wp_genesis 16 *
mp-wp_genesis 17 * {@internal
mp-wp_genesis 18 * Please be aware that the above note was made during the beta of WordPress 2.6
mp-wp_genesis 19 * and in the future may not be accurate. Please update the note when it is no
mp-wp_genesis 20 * longer the case.}}
mp-wp_genesis 21 *
mp-wp_genesis 22 * To apply shortcode tags to content:
mp-wp_genesis 23 *
mp-wp_genesis 24 * <code>
mp-wp_genesis 25 * $out = do_shortcode($content);
mp-wp_genesis 26 * </code>
mp-wp_genesis 27 *
mp-wp_genesis 28 * @link http://codex.wordpress.org/Shortcode_API
mp-wp_genesis 29 *
mp-wp_genesis 30 * @package WordPress
mp-wp_genesis 31 * @subpackage Shortcodes
mp-wp_genesis 32 * @since 2.5
mp-wp_genesis 33 */
mp-wp_genesis 34
mp-wp_genesis 35 /**
mp-wp_genesis 36 * Container for storing shortcode tags and their hook to call for the shortcode
mp-wp_genesis 37 *
mp-wp_genesis 38 * @since 2.5
mp-wp_genesis 39 * @name $shortcode_tags
mp-wp_genesis 40 * @var array
mp-wp_genesis 41 * @global array $shortcode_tags
mp-wp_genesis 42 */
mp-wp_genesis 43 $shortcode_tags = array();
mp-wp_genesis 44
mp-wp_genesis 45 /**
mp-wp_genesis 46 * Add hook for shortcode tag.
mp-wp_genesis 47 *
mp-wp_genesis 48 * There can only be one hook for each shortcode. Which means that if another
mp-wp_genesis 49 * plugin has a similar shortcode, it will override yours or yours will override
mp-wp_genesis 50 * theirs depending on which order the plugins are included and/or ran.
mp-wp_genesis 51 *
mp-wp_genesis 52 * Simplest example of a shortcode tag using the API:
mp-wp_genesis 53 *
mp-wp_genesis 54 * <code>
mp-wp_genesis 55 * // [footag foo="bar"]
mp-wp_genesis 56 * function footag_func($atts) {
mp-wp_genesis 57 * return "foo = {$atts[foo]}";
mp-wp_genesis 58 * }
mp-wp_genesis 59 * add_shortcode('footag', 'footag_func');
mp-wp_genesis 60 * </code>
mp-wp_genesis 61 *
mp-wp_genesis 62 * Example with nice attribute defaults:
mp-wp_genesis 63 *
mp-wp_genesis 64 * <code>
mp-wp_genesis 65 * // [bartag foo="bar"]
mp-wp_genesis 66 * function bartag_func($atts) {
mp-wp_genesis 67 * extract(shortcode_atts(array(
mp-wp_genesis 68 * 'foo' => 'no foo',
mp-wp_genesis 69 * 'baz' => 'default baz',
mp-wp_genesis 70 * ), $atts));
mp-wp_genesis 71 *
mp-wp_genesis 72 * return "foo = {$foo}";
mp-wp_genesis 73 * }
mp-wp_genesis 74 * add_shortcode('bartag', 'bartag_func');
mp-wp_genesis 75 * </code>
mp-wp_genesis 76 *
mp-wp_genesis 77 * Example with enclosed content:
mp-wp_genesis 78 *
mp-wp_genesis 79 * <code>
mp-wp_genesis 80 * // [baztag]content[/baztag]
mp-wp_genesis 81 * function baztag_func($atts, $content='') {
mp-wp_genesis 82 * return "content = $content";
mp-wp_genesis 83 * }
mp-wp_genesis 84 * add_shortcode('baztag', 'baztag_func');
mp-wp_genesis 85 * </code>
mp-wp_genesis 86 *
mp-wp_genesis 87 * @since 2.5
mp-wp_genesis 88 * @uses $shortcode_tags
mp-wp_genesis 89 *
mp-wp_genesis 90 * @param string $tag Shortcode tag to be searched in post content.
mp-wp_genesis 91 * @param callable $func Hook to run when shortcode is found.
mp-wp_genesis 92 */
mp-wp_genesis 93 function add_shortcode($tag, $func) {
mp-wp_genesis 94 global $shortcode_tags;
mp-wp_genesis 95
mp-wp_genesis 96 if ( is_callable($func) )
mp-wp_genesis 97 $shortcode_tags[$tag] = $func;
mp-wp_genesis 98 }
mp-wp_genesis 99
mp-wp_genesis 100 /**
mp-wp_genesis 101 * Removes hook for shortcode.
mp-wp_genesis 102 *
mp-wp_genesis 103 * @since 2.5
mp-wp_genesis 104 * @uses $shortcode_tags
mp-wp_genesis 105 *
mp-wp_genesis 106 * @param string $tag shortcode tag to remove hook for.
mp-wp_genesis 107 */
mp-wp_genesis 108 function remove_shortcode($tag) {
mp-wp_genesis 109 global $shortcode_tags;
mp-wp_genesis 110
mp-wp_genesis 111 unset($shortcode_tags[$tag]);
mp-wp_genesis 112 }
mp-wp_genesis 113
mp-wp_genesis 114 /**
mp-wp_genesis 115 * Clear all shortcodes.
mp-wp_genesis 116 *
mp-wp_genesis 117 * This function is simple, it clears all of the shortcode tags by replacing the
mp-wp_genesis 118 * shortcodes global by a empty array. This is actually a very efficient method
mp-wp_genesis 119 * for removing all shortcodes.
mp-wp_genesis 120 *
mp-wp_genesis 121 * @since 2.5
mp-wp_genesis 122 * @uses $shortcode_tags
mp-wp_genesis 123 */
mp-wp_genesis 124 function remove_all_shortcodes() {
mp-wp_genesis 125 global $shortcode_tags;
mp-wp_genesis 126
mp-wp_genesis 127 $shortcode_tags = array();
mp-wp_genesis 128 }
mp-wp_genesis 129
mp-wp_genesis 130 /**
mp-wp_genesis 131 * Search content for shortcodes and filter shortcodes through their hooks.
mp-wp_genesis 132 *
mp-wp_genesis 133 * If there are no shortcode tags defined, then the content will be returned
mp-wp_genesis 134 * without any filtering. This might cause issues when plugins are disabled but
mp-wp_genesis 135 * the shortcode will still show up in the post or content.
mp-wp_genesis 136 *
mp-wp_genesis 137 * @since 2.5
mp-wp_genesis 138 * @uses $shortcode_tags
mp-wp_genesis 139 * @uses get_shortcode_regex() Gets the search pattern for searching shortcodes.
mp-wp_genesis 140 *
mp-wp_genesis 141 * @param string $content Content to search for shortcodes
mp-wp_genesis 142 * @return string Content with shortcodes filtered out.
mp-wp_genesis 143 */
mp-wp_genesis 144 function do_shortcode($content) {
mp-wp_genesis 145 global $shortcode_tags;
mp-wp_genesis 146
mp-wp_genesis 147 if (empty($shortcode_tags) || !is_array($shortcode_tags))
mp-wp_genesis 148 return $content;
mp-wp_genesis 149
mp-wp_genesis 150 $pattern = get_shortcode_regex();
mp-wp_genesis 151 return preg_replace_callback('/'.$pattern.'/s', 'do_shortcode_tag', $content);
mp-wp_genesis 152 }
mp-wp_genesis 153
mp-wp_genesis 154 /**
mp-wp_genesis 155 * Retrieve the shortcode regular expression for searching.
mp-wp_genesis 156 *
mp-wp_genesis 157 * The regular expression combines the shortcode tags in the regular expression
mp-wp_genesis 158 * in a regex class.
mp-wp_genesis 159 *
mp-wp_genesis 160 * @since 2.5
mp-wp_genesis 161 * @uses $shortcode_tags
mp-wp_genesis 162 *
mp-wp_genesis 163 * @return string The shortcode search regular expression
mp-wp_genesis 164 */
mp-wp_genesis 165 function get_shortcode_regex() {
mp-wp_genesis 166 global $shortcode_tags;
mp-wp_genesis 167 $tagnames = array_keys($shortcode_tags);
mp-wp_genesis 168 $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
mp-wp_genesis 169
mp-wp_genesis 170 return '\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?';
mp-wp_genesis 171 }
mp-wp_genesis 172
mp-wp_genesis 173 /**
mp-wp_genesis 174 * Regular Expression callable for do_shortcode() for calling shortcode hook.
mp-wp_genesis 175 *
mp-wp_genesis 176 * @since 2.5
mp-wp_genesis 177 * @access private
mp-wp_genesis 178 * @uses $shortcode_tags
mp-wp_genesis 179 *
mp-wp_genesis 180 * @param array $m Regular expression match array
mp-wp_genesis 181 * @return mixed False on failure.
mp-wp_genesis 182 */
mp-wp_genesis 183 function do_shortcode_tag($m) {
mp-wp_genesis 184 global $shortcode_tags;
mp-wp_genesis 185
mp-wp_genesis 186 $tag = $m[1];
mp-wp_genesis 187 $attr = shortcode_parse_atts($m[2]);
mp-wp_genesis 188
mp-wp_genesis 189 if ( isset($m[4]) ) {
mp-wp_genesis 190 // enclosing tag - extra parameter
mp-wp_genesis 191 return call_user_func($shortcode_tags[$tag], $attr, $m[4], $tag);
mp-wp_genesis 192 } else {
mp-wp_genesis 193 // self-closing tag
mp-wp_genesis 194 return call_user_func($shortcode_tags[$tag], $attr, NULL, $tag);
mp-wp_genesis 195 }
mp-wp_genesis 196 }
mp-wp_genesis 197
mp-wp_genesis 198 /**
mp-wp_genesis 199 * Retrieve all attributes from the shortcodes tag.
mp-wp_genesis 200 *
mp-wp_genesis 201 * The attributes list has the attribute name as the key and the value of the
mp-wp_genesis 202 * attribute as the value in the key/value pair. This allows for easier
mp-wp_genesis 203 * retrieval of the attributes, since all attributes have to be known.
mp-wp_genesis 204 *
mp-wp_genesis 205 * @since 2.5
mp-wp_genesis 206 *
mp-wp_genesis 207 * @param string $text
mp-wp_genesis 208 * @return array List of attributes and their value.
mp-wp_genesis 209 */
mp-wp_genesis 210 function shortcode_parse_atts($text) {
mp-wp_genesis 211 $atts = array();
mp-wp_genesis 212 $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
mp-wp_genesis 213 $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
mp-wp_genesis 214 if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
mp-wp_genesis 215 foreach ($match as $m) {
mp-wp_genesis 216 if (!empty($m[1]))
mp-wp_genesis 217 $atts[strtolower($m[1])] = stripcslashes($m[2]);
mp-wp_genesis 218 elseif (!empty($m[3]))
mp-wp_genesis 219 $atts[strtolower($m[3])] = stripcslashes($m[4]);
mp-wp_genesis 220 elseif (!empty($m[5]))
mp-wp_genesis 221 $atts[strtolower($m[5])] = stripcslashes($m[6]);
mp-wp_genesis 222 elseif (isset($m[7]) and strlen($m[7]))
mp-wp_genesis 223 $atts[] = stripcslashes($m[7]);
mp-wp_genesis 224 elseif (isset($m[8]))
mp-wp_genesis 225 $atts[] = stripcslashes($m[8]);
mp-wp_genesis 226 }
mp-wp_genesis 227 } else {
mp-wp_genesis 228 $atts = ltrim($text);
mp-wp_genesis 229 }
mp-wp_genesis 230 return $atts;
mp-wp_genesis 231 }
mp-wp_genesis 232
mp-wp_genesis 233 /**
mp-wp_genesis 234 * Combine user attributes with known attributes and fill in defaults when needed.
mp-wp_genesis 235 *
mp-wp_genesis 236 * The pairs should be considered to be all of the attributes which are
mp-wp_genesis 237 * supported by the caller and given as a list. The returned attributes will
mp-wp_genesis 238 * only contain the attributes in the $pairs list.
mp-wp_genesis 239 *
mp-wp_genesis 240 * If the $atts list has unsupported attributes, then they will be ignored and
mp-wp_genesis 241 * removed from the final returned list.
mp-wp_genesis 242 *
mp-wp_genesis 243 * @since 2.5
mp-wp_genesis 244 *
mp-wp_genesis 245 * @param array $pairs Entire list of supported attributes and their defaults.
mp-wp_genesis 246 * @param array $atts User defined attributes in shortcode tag.
mp-wp_genesis 247 * @return array Combined and filtered attribute list.
mp-wp_genesis 248 */
mp-wp_genesis 249 function shortcode_atts($pairs, $atts) {
mp-wp_genesis 250 $atts = (array)$atts;
mp-wp_genesis 251 $out = array();
mp-wp_genesis 252 foreach($pairs as $name => $default) {
mp-wp_genesis 253 if ( array_key_exists($name, $atts) )
mp-wp_genesis 254 $out[$name] = $atts[$name];
mp-wp_genesis 255 else
mp-wp_genesis 256 $out[$name] = $default;
mp-wp_genesis 257 }
mp-wp_genesis 258 return $out;
mp-wp_genesis 259 }
mp-wp_genesis 260
mp-wp_genesis 261 /**
mp-wp_genesis 262 * Remove all shortcode tags from the given content.
mp-wp_genesis 263 *
mp-wp_genesis 264 * @since 2.5
mp-wp_genesis 265 * @uses $shortcode_tags
mp-wp_genesis 266 *
mp-wp_genesis 267 * @param string $content Content to remove shortcode tags.
mp-wp_genesis 268 * @return string Content without shortcode tags.
mp-wp_genesis 269 */
mp-wp_genesis 270 function strip_shortcodes( $content ) {
mp-wp_genesis 271 global $shortcode_tags;
mp-wp_genesis 272
mp-wp_genesis 273 if (empty($shortcode_tags) || !is_array($shortcode_tags))
mp-wp_genesis 274 return $content;
mp-wp_genesis 275
mp-wp_genesis 276 $pattern = get_shortcode_regex();
mp-wp_genesis 277
mp-wp_genesis 278 return preg_replace('/'.$pattern.'/s', '', $content);
mp-wp_genesis 279 }
mp-wp_genesis 280
mp-wp_genesis 281 add_filter('the_content', 'do_shortcode', 11); // AFTER wpautop()
mp-wp_genesis 282
mp-wp_genesis 283 ?>