【发布时间】:2010-07-07 01:03:04
【问题描述】:
在 WP 中,您可以从字符串中过滤短代码并使用 do_shortcode($string) 执行其挂钩函数。
是否可以过滤单个简码,而不是所有已注册的简码?
例如,我还需要一些简码可用于评论海报,但并非全部都是因为显而易见的原因 :)
【问题讨论】:
在 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;
}
这通过过滤全局$shortcode_tags,运行do_shortcode(),然后将所有内容恢复到以前的状态来工作。
使用示例;
$comment = do_shortcode_by_tags($comment, array('tag_1', 'tag_2'));
这会将简码 tag_1 和 tag_2 应用于评论。
【讨论】: