简体   繁体   中英

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 posters 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM