wordpress短代码的想法

问题描述:

我有一个关于在我的网站上生成“wordpress短代码”代码的问题。有人知道他们是如何在wordpress中完成短代码的吗?我有那么有一个字符串变量里面的内容的一个想法检查,如果字符串有我的示例中列出短代码:wordpress短代码的想法

$x = "there should be a shortcode here [theshortcode]"; 
//if there is a match 
//update the x string add the return of the shortcode [the shortcode] 
//then display it using echo 

这是正确的观念还是没有人知道如何落后的wordpress简码的概念?

(注:我没有使用WordPress的,我发展我自己的网站使用的WordPress的简码的概念)

+0

http://codex.wordpress.org/Sortort_API – mschuett 2014-09-26 19:52:11

+0

我问他们如何在PHP中实现简码,而不是问如何使用wordpress的简码。我试图开发一个网站,使用短代码,而不是使用wordpress – Cliffmeister 2014-09-26 19:54:00

+0

o那么请更新你的问题,因为你刚才所说,你发布的是一个完全不同的问题。 – mschuett 2014-09-26 19:55:41

https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/shortcodes.php#L228包括用于分析的简码的代码...

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]] 
} 

我相信这是你想要的,因为它显示了他们目前是如何解析的短代码,并在此行他们实现调用函数https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/shortcodes.php#L277 ...

function do_shortcode_tag($m) { 
    global $shortcode_tags; 

    // allow [[foo]] syntax for escaping a tag 
    if ($m[1] == '[' && $m[6] == ']') { 
     return substr($m[0], 1, -1); 
    } 

    $tag = $m[2]; 
    $attr = shortcode_parse_atts($m[3]); 

    if (isset($m[5])) { 
     // enclosing tag - extra parameter 
     return $m[1] . call_user_func($shortcode_tags[$tag], $attr, $m[5], $tag) . $m[6]; 
    } else { 
     // self-closing tag 
     return $m[1] . call_user_func($shortcode_tags[$tag], $attr, null, $tag) . $m[6]; 
    } 
} 

说实话,我只是下载wordpress的一个副本,并窃取wp-includes/shortcodes.php文件,并节省自己的麻烦或重新实现这一点。

+1

我删除了我的答案,因为它基本上和你的一样。我投了你的答案,因为我认为这是问题的正确答案。我不明白downvote .. – bitWorking 2014-09-26 20:20:11