WordPress.org

Make WordPress Core

Changeset 10576


Ignore:
Timestamp:
02/15/2009 04:01:14 PM (9 years ago)
Author:
westi
Message:

Introduce a simple mechanism for escaping shortcodes by doubling the [[]]. Fixes #6518 props tellyworth.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/shortcodes.php

    r8613 r10576  
    157157 * The regular expression combines the shortcode tags in the regular expression
    158158 * in a regex class.
    159  *
     159 *
     160 * The regular expresion contains 6 different sub matches to help with parsing.
     161 *
     162 * 1/6 - An extra [ or ] 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 *
    160168 * @since 2.5
    161169 * @uses $shortcode_tags
     
    168176    $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
    169177
    170     return '\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\1\])?';
     178    return '(.?)\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)';
    171179}
    172180
    173181/**
    174182 * Regular Expression callable for do_shortcode() for calling shortcode hook.
     183 * @see get_shortcode_regex for details of the match array contents.
    175184 *
    176185 * @since 2.5
     
    184193    global $shortcode_tags;
    185194
    186     $tag = $m[1];
    187     $attr = shortcode_parse_atts($m[2]);
    188 
    189     if ( isset($m[4]) ) {
     195    // allow [[foo]] syntax for escaping a tag
     196    if ($m[1] == '[' && $m[6] == ']') {
     197        return substr($m[0], 1, -1);
     198    }
     199   
     200    $tag = $m[2];
     201    $attr = shortcode_parse_atts($m[3]);
     202
     203    if ( isset($m[5]) ) {
    190204        // enclosing tag - extra parameter
    191         return call_user_func($shortcode_tags[$tag], $attr, $m[4], $tag);
     205        return $m[1] . call_user_func($shortcode_tags[$tag], $attr, $m[5]) . $m[6];
    192206    } else {
    193207        // self-closing tag
    194         return call_user_func($shortcode_tags[$tag], $attr, NULL, $tag);
     208        return $m[1] . call_user_func($shortcode_tags[$tag], $attr) . $m[6];
    195209    }
    196210}
Note: See TracChangeset for help on using the changeset viewer.