Php wordpress短代码的概念
我有一个关于在我的网站上生成“类似wordpress的短代码”的问题。有人知道他们是如何在wordpress中使用短代码的吗?我的想法是将内容放在字符串变量中,然后检查字符串是否有我列出的短代码,例如:Php wordpress短代码的概念,php,wordpress,Php,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]
$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的短代码概念)包括用于解析短代码的代码
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]]
}
我相信这就是你想要的,因为它显示了他们目前是如何解析短代码的,并且在这一行他们实现了调用函数
老实说,我只想下载wordpress的一个副本,然后偷取wp includes/shortcodes.php文件,省去麻烦或者重新实现它。我是问他们是如何在php中实现shortcode的,而不是问如何使用wordpress短代码。我正在尝试开发一个使用短代码而不使用wordpresso的网站,然后请更新您的问题,因为您刚才所说的和您发布的是一个完全不同的问题。我相信这就是你想要的,因为它显示了他们目前是如何解析短代码的,在这一行他们实现了调用函数,我删除了我的答案,因为它基本上与你的相同。我对你的答案投了赞成票,因为我认为这是这个问题的正确答案。我不明白投反对票的原因。。
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];
}
}