【发布时间】:2016-11-02 21:35:04
【问题描述】:
我将编写一些示例代码,因此这是我遇到的问题的简短示例。
假设我在数据库中存储了以下文本:
[form]
<ul>
[each name="upgrades"]
<li><input type="checkbox" [value name="upgrade_name" id="1"] />[value name="upgrade_name" id="2"]</li>
[/each]
</ul>
[/form]
如果我在此文本上运行 do_shortcode,则会解析 each 内容内的短代码 INSIDE html 标签,而不是推迟到 each 短代码。但是,在 each 短代码在其内容上运行 do_shortcode 之前,不会解析不在 each 内容中的 html 标记中的短代码,这应该是正确的行为。
换句话说,ID为1的value简码被解析得太快(在form简码传递中),但ID为2的value简码直到each简码才被解析在其上运行do_shortcode,因此它会产生正确的值。
我知道我可以将表单简码上的 ignore_html 标志设置为 true,但这是不正确的,因为用户可能希望为简码解析 html 标记。
是否有解决此问题的方法?
Wordpress 版本 4.6.1
编辑:添加可重现的代码
使用此代码创建一个新插件:
<?php
/*
Plugin Name: Broken Shortcodes
Description: Shortcodes should not jump the gun in parsing html tag shortcodes of inner shortcode content.
*/
remove_filter('the_content', 'wpautop');
add_shortcode('form', function($atts, $content){
echo "<textarea>This is the form's content:\n".$content.'</textarea>';
return "<textarea>This is the rendered form shortcode:\n".do_shortcode($content).'</textarea>';
});
$bad_global_variable = 'first';
add_shortcode('value', function($atts, $content){
global $bad_global_variable;
return $bad_global_variable;
});
add_shortcode('each', function($atts, $content){
global $bad_global_variable;
$_content = '';
foreach(array('second', 'third', 'fourth') as $v){
$bad_global_variable = $v;
$_content .= do_shortcode($content);
}
return $_content;
});
?>
使用此文本创建一个页面:
[form]
[each]
<div [value]>[value]</div>
[/each]
[/form]
输出不正确:
<div first>second</div>
<div first>third</div>
<div first>second</div>
<div first>fourth</div>
<div first>second</div>
<div first>third</div>
<div first>second</div>
【问题讨论】:
标签: wordpress