是否可以嵌套具有相同短代码的 wordpress 短代码?

tsd*_*ter 5 php wordpress shortcode

我知道如果使用 do_shortcode 包装器,则可以嵌套短代码,但是,codex 指出:

“但是,如果使用短代码宏包含另一个同名宏,解析器将失败:”

有没有解决的办法?

例如,如果我有一个短代码来制作一个 div,例如:

[div]some content in a div[/div]
Run Code Online (Sandbox Code Playgroud)

我希望能够使用:

[div]
    [div]a nested div[/div]
[/div]
Run Code Online (Sandbox Code Playgroud)

但这将因标准 do_shortcode 包装器而失败。

我的临时解决方法是复制带有 _parent 附加到名称的短代码,但除非我创建了 div_parent1、div_parent2 等,否则我只能嵌套 1 级深...

dou*_*arp 1

API 是这样告诉它的,因此这是不可能的:

This is a limitation of the context-free regexp parser used by do_shortcode() - it is very fast but does not count levels of nesting, so it can't match each opening tag with its correct closing tag in these cases.

最新版本(3.4.2)中的相关功能是:

function do_shortcode($content) {
    global $shortcode_tags;

    if (empty($shortcode_tags) || !is_array($shortcode_tags))
        return $content;

    $pattern = get_shortcode_regex();
    return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
}

function get_shortcode_regex() {
    global $shortcode_tags;
    $tagnames = array_keys($shortcode_tags);
    $tagregexp = join( '|', array_map('preg_quote', $tagnames) );

    // WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
    return
          '\\['                              // Opening bracket
        . '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
        . "($tagregexp)"                     // 2: Shortcode name
        . '\\b'                              // Word boundary
        . '('                                // 3: Unroll the loop: Inside the opening shortcode tag
        .     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
        .     '(?:'
        .         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
        .         '[^\\]\\/]*'               // Not a closing bracket or forward slash
        .     ')*?'
        . ')'
        . '(?:'
        .     '(\\/)'                        // 4: Self closing tag ...
        .     '\\]'                          // ... and closing bracket
        . '|'
        .     '\\]'                          // Closing bracket
        .     '(?:'
        .         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
        .             '[^\\[]*+'             // Not an opening bracket
        .             '(?:'
        .                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
        .                 '[^\\[]*+'         // Not an opening bracket
        .             ')*+'
        .         ')'
        .         '\\[\\/\\2\\]'             // Closing shortcode tag
        .     ')?'
        . ')'
        . '(\\]?)';                          // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
}
Run Code Online (Sandbox Code Playgroud)