Wordpress / PHP - Make One Short Code

In WP, you can filter short strings from a string and execute their connected functions with do_shortcode($string).

Is it possible to filter out one short code instead of all registered short codes?

For example, I need some short codes that will be available for comment posters, but not all for obvious reasons :)

+3
source share
1 answer
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, starting do_shortcode()and then returning everything as it was before.

Usage example;

$comment = do_shortcode_by_tags($comment, array('tag_1', 'tag_2'));

This will apply the short code tag_1and to the comment tag_2.

+2
source

Source: https://habr.com/ru/post/1753445/


All Articles