Wordpress / PHP - Do a single shortcode
In WP you can filter shortcodes from a string and execute their hooked functions with do_shortcode($string)
.
Is it possible to filter a single shortcode, instead of all registered shortcodes?
for example I need a few shortcodes to be available for comment poste开发者_StackOverflow社区rs as well, but not all for obvious reasons :)
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;
}
This works by filtering the global $shortcode_tags
, running do_shortcode()
, then putting everything back as it was before.
Example use;
$comment = do_shortcode_by_tags($comment, array('tag_1', 'tag_2'));
This will apply the shortcode tag_1
and tag_2
to the comment.
精彩评论