2014-09-26 87 views
-2

我有一個關於在我的網站上生成「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

回答

3

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