最近,我一直在研究一些短代码,其中短代码生成的所有元素都封装在一个标记中。不幸的是,这并不像所希望的那样呈现,因为在\\u内容上有过滤器wpautop,或者您正在使用的任何东西来显示短代码和/或常规内容。
设置设想我们创建了以下短代码:
<?php
function example_shortcode($atts, $content = null) {
extract(shortcode_atts(
array(
\'before\' => \'\',
\'after\' => \'\',
\'link\' => \'\',
), $atts)
);
$before = (!empty($atts[\'before\'])) ? \'<div class="before">\'.$atts[\'before\'].\'</div>\' : \'\';
$after = (!empty($atts[\'after\'])) ? \'<div class="after">\'.$atts[\'after\'].\'</div>\' : \'\';
$link = (!empty($atts[\'link\'])) ? \' href="\'.$atts[\'link\'].\'"\' : \'\';
return \'<a\'.$link.\'>\'.$before.do_shortcode($content).$after.\'</a>\';
}
add_shortcode(\'example_shortcode\', \'example_shortcode\');
我们还创建了一个包含以下内容的页面。
some text before the shortcode
[example_shortcode before="Welcome" after="Goodbye" link="#"]
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
[/example_shortcode]
some text after the shortcode
测试
wpautop 使用默认优先级添加的过滤器输出如下所示:
<p>some text before the shortcode</p>
<a href="#"><div class="before">Welcome</div></p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<p><div class="after">Goodbye</div></a>
<p>some text after the shortcode</p>
或与
remove_filter(\'the_content\', \'wpautop\');
添加后,它还会返回正确的HTML:
some text before the shortcode
<a href="#"><div class="before">Welcome</div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
<div class="after">Goodbye</div></a>
some text after the shortcode
但是当wpautop过滤器添加了更高的优先级时,例如。。。
<?php
remove_filter(\'the_content\', \'wpautop\');
add_filter(\'the_content\', \'wpautop\', PHP_INT_MAX);
然后输出返回所有混乱的结果,如下所示:
<p>some text before the shortcode</p>
<p><a href="#">
</a></p><div class="before"><a href="#">Welcome</a></div><a href="#">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<div class="after">Goodbye</div>
</a><p><a href="#"></a></p>
<p>some text after the shortcode</p>
问题和我的目标为什么
<a>
标签添加了多次,但只应打开和关闭一次?
这是我想要呈现的HTML,在更改<a>
example\\u shortcode函数内的标记,例如a<div>
标签为什么这些标签的输出不同?
<p>some text before the shortcode</p>
<a href="#">
<div class="before">Welcome</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<div class="after">Goodbye</div>
</a>
<p>some text after the shortcode</p>