| 2 | | |
|---|
| 3 | | /* |
|---|
| 4 | | |
|---|
| 5 | | An API for creating shortcode tags that support attributes and enclosed content, such as: |
|---|
| 6 | | |
|---|
| 7 | | [shortcode /] |
|---|
| 8 | | [shortcode foo="bar" baz="bing" /] |
|---|
| 9 | | [shortcode foo="bar"]content[/shortcode] |
|---|
| 10 | | |
|---|
| 11 | | tag and attrbute parsing regexp code based on the Textpattern tag parser. |
|---|
| 12 | | |
|---|
| 13 | | To apply shortcode tags to content: |
|---|
| 14 | | |
|---|
| 15 | | $out = do_shortcode($content); |
|---|
| 16 | | |
|---|
| 17 | | Simplest example of a shortcode tag using the API: |
|---|
| 18 | | |
|---|
| 19 | | // [footag foo="bar"] |
|---|
| 20 | | function footag_func($atts) { |
|---|
| 21 | | return "foo = {$atts[foo]}"; |
|---|
| 22 | | } |
|---|
| 23 | | add_shortcode('footag', 'footag_func'); |
|---|
| 24 | | |
|---|
| 25 | | Example with nice attribute defaults: |
|---|
| 26 | | |
|---|
| 27 | | // [bartag foo="bar"] |
|---|
| 28 | | function bartag_func($atts) { |
|---|
| 29 | | extract(shortcode_atts(array( |
|---|
| 30 | | 'foo' => 'no foo', |
|---|
| 31 | | 'baz' => 'default baz', |
|---|
| 32 | | ), $atts)); |
|---|
| 33 | | |
|---|
| 34 | | return "foo = {$foo}"; |
|---|
| 35 | | } |
|---|
| 36 | | add_shortcode('bartag', 'bartag_func'); |
|---|
| 37 | | |
|---|
| 38 | | Example with enclosed content: |
|---|
| 39 | | |
|---|
| 40 | | // [baztag]content[/baztag] |
|---|
| 41 | | function baztag_func($atts, $content='') { |
|---|
| 42 | | return "content = $content"; |
|---|
| 43 | | } |
|---|
| 44 | | add_shortcode('baztag', 'baztag_func'); |
|---|
| 45 | | |
|---|
| 46 | | */ |
|---|
| 47 | | |
|---|
| | 2 | /** |
|---|
| | 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. |
|---|
| | 6 | * |
|---|
| | 7 | * A few examples are below: |
|---|
| | 8 | * |
|---|
| | 9 | * [shortcode /] |
|---|
| | 10 | * [shortcode foo="bar" baz="bing" /] |
|---|
| | 11 | * [shortcode foo="bar"]content[/shortcode] |
|---|
| | 12 | * |
|---|
| | 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. |
|---|
| | 16 | * |
|---|
| | 17 | * {@internal |
|---|
| | 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 |
|---|
| | 20 | * longer the case.}} |
|---|
| | 21 | * |
|---|
| | 22 | * To apply shortcode tags to content: |
|---|
| | 23 | * |
|---|
| | 24 | * <code> |
|---|
| | 25 | * $out = do_shortcode($content); |
|---|
| | 26 | * </code> |
|---|
| | 27 | * |
|---|
| | 28 | * @link http://codex.wordpress.org/Shortcode_API |
|---|
| | 29 | * |
|---|
| | 30 | * @package WordPress |
|---|
| | 31 | * @subpackage Shortcodes |
|---|
| | 32 | * @since 2.5 |
|---|
| | 33 | */ |
|---|
| | 34 | |
|---|
| | 35 | /** |
|---|
| | 36 | * Container for storing shortcode tags and their hook to call for the shortcode |
|---|
| | 37 | * |
|---|
| | 38 | * @since 2.5 |
|---|
| | 39 | * @name $shortcode_tags |
|---|
| | 40 | * @var array |
|---|
| | 41 | * @global array $shortcode_tags |
|---|
| | 42 | */ |
|---|
| | 45 | /** |
|---|
| | 46 | * Add hook for shortcode tag. |
|---|
| | 47 | * |
|---|
| | 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. |
|---|
| | 51 | * |
|---|
| | 52 | * Simplest example of a shortcode tag using the API: |
|---|
| | 53 | * |
|---|
| | 54 | * <code> |
|---|
| | 55 | * // [footag foo="bar"] |
|---|
| | 56 | * function footag_func($atts) { |
|---|
| | 57 | * return "foo = {$atts[foo]}"; |
|---|
| | 58 | * } |
|---|
| | 59 | * add_shortcode('footag', 'footag_func'); |
|---|
| | 60 | * </code> |
|---|
| | 61 | * |
|---|
| | 62 | * Example with nice attribute defaults: |
|---|
| | 63 | * |
|---|
| | 64 | * <code> |
|---|
| | 65 | * // [bartag foo="bar"] |
|---|
| | 66 | * function bartag_func($atts) { |
|---|
| | 67 | * extract(shortcode_atts(array( |
|---|
| | 68 | * 'foo' => 'no foo', |
|---|
| | 69 | * 'baz' => 'default baz', |
|---|
| | 70 | * ), $atts)); |
|---|
| | 71 | * |
|---|
| | 72 | * return "foo = {$foo}"; |
|---|
| | 73 | * } |
|---|
| | 74 | * add_shortcode('bartag', 'bartag_func'); |
|---|
| | 75 | * </code> |
|---|
| | 76 | * |
|---|
| | 77 | * Example with enclosed content: |
|---|
| | 78 | * |
|---|
| | 79 | * <code> |
|---|
| | 80 | * // [baztag]content[/baztag] |
|---|
| | 81 | * function baztag_func($atts, $content='') { |
|---|
| | 82 | * return "content = $content"; |
|---|
| | 83 | * } |
|---|
| | 84 | * add_shortcode('baztag', 'baztag_func'); |
|---|
| | 85 | * </code> |
|---|
| | 86 | * |
|---|
| | 87 | * @since 2.5 |
|---|
| | 88 | * @uses $shortcode_tags |
|---|
| | 89 | * |
|---|
| | 90 | * @param string $tag Shortcode tag to be searched in post content. |
|---|
| | 91 | * @param callable $func Hook to run when shortcode is found. |
|---|
| | 92 | */ |
|---|
| | 233 | /** |
|---|
| | 234 | * Combine user attributes with known attributes and fill in defaults when needed. |
|---|
| | 235 | * |
|---|
| | 236 | * The pairs should be considered to be all of the attributes which are |
|---|
| | 237 | * supported by the caller and given as a list. The returned attributes will |
|---|
| | 238 | * only contain the attributes in the $pairs list. |
|---|
| | 239 | * |
|---|
| | 240 | * If the $atts list has unsupported attributes, then they will be ignored and |
|---|
| | 241 | * removed from the final returned list. |
|---|
| | 242 | * |
|---|
| | 243 | * @since 2.5 |
|---|
| | 244 | * |
|---|
| | 245 | * @param array $pairs Entire list of supported attributes and their defaults. |
|---|
| | 246 | * @param array $atts User defined attributes in shortcode tag. |
|---|
| | 247 | * @return array Combined and filtered attribute list. |
|---|
| | 248 | */ |
|---|