短码是否区分大小写?

时间:2013-06-09 作者:Ben Miller - Remember Monica

如果我编写插件并使用以下代码注册短代码:

add_shortcode(\'footag\', \'footag_func\');
用户在帖子中使用:

[FOOTAG]
它应该有用吗?

1 个回复
最合适的回答,由SO网友:chrisguitarguy 整理而成

简短回答是,短代码区分大小写

回答得更长一些test case 对此,请参见。

<?php
add_shortcode(\'sOme_ShOrTcOdE\', \'wpse102375_shortcode\');
function wpse102375_shortcode($args, $content=null)
{
    return \'yep\';
}

最长答案

读取源代码。

短代码的“魔力”发生在do_shortcode, 让我们看看这个。

<?php
// wp-includes/shortcode.php
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 );
}
嗯,嗯,$shortcode_tags -- 可能是在add_shortcode:

<?php
// wp-includes/shortcodes.php
function add_shortcode($tag, $func) {
    global $shortcode_tags;

    if ( is_callable($func) )
        $shortcode_tags[$tag] = $func;
}
所以这只是$shortcode_name => $a_callablle. 有道理。

看起来最神奇的do_shortcode 在构建正则表达式以匹配短代码本身时发生。所有这些都发生在get_shortcode_regex, 那么让我们看看这里:

<?php
// wp-includes/shortcode.php
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()
    // Also, see shortcode_unautop() and shortcode.js.
    return
          \'\\\\[\'                              // Opening bracket
        . \'(\\\\[?)\'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
        . "($tagregexp)"                     // 2: Shortcode name
        . \'(?![\\\\w-])\'                       // Not followed by word character or hyphen
        . \'(\'                                // 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]]
}
核心评论很好,所以这里没有太多解释。关键在于:

global $shortcode_tags;
$tagnames = array_keys($shortcode_tags);
$tagregexp = join( \'|\', array_map(\'preg_quote\', $tagnames) );
它本质上将在以后用于显式匹配任何已注册的短代码名称。请注意,除了preg_quote, 因此,WP将只尝试匹配传入的显式值add_shortcode 作为短代码名称。看起来像是短代码are case sensitive 目前为止

接下来,我们需要查看正则表达式内置的标志get_shortcode_regex 已使用。的相关位do_shortcode.

"/$pattern/s"
斜杠分隔正则表达式,右斜杠后的字母为PRCE flags or modifiers.

如果我们看到i 在那里,它不区分大小写。我们只有s (指a. 匹配所有字符(包括换行符)。

所以,是的,shortcodes are case sensitive.

结束

相关推荐

Shortcode of a function

我得到了这个函数,我想将“foreach”的contents divs作为return。我该怎么做?<?php $args = array( \'numberposts\' => 6, \'post_status\'=>\"publish\",\'post_type\'=>\"post\",\'orderby\'=>\"post_date\"); $postslist = get_posts( $args ); fo