在 WP 中,您可以从字符串中过滤短代码并使用do_shortcode($string)
.
是否可以过滤单个简码,而不是所有已注册的简码?
例如,我还需要一些简码可用于评论海报,但出于明显的原因,并非全部:)
function do_shortcode_by_tags($content, $tags)
{
global $shortcode_tags;
$_tags = $shortcode_tags; // store temp copy
foreach ($_tags as $tag => $callback) {
if (!in_array($tag, $tags)) // filter unwanted shortcode
unset($shortcode_tags[$tag]);
}
$shortcoded = do_shortcode($content);
$shortcode_tags = $_tags; // put all shortcode back
return $shortcoded;
}
这通过过滤 global $shortcode_tags
, running do_shortcode()
,然后将所有内容恢复原样来工作。
示例使用;
$comment = do_shortcode_by_tags($comment, array('tag_1', 'tag_2'));
这将应用简码tag_1
和tag_2
评论。