简体   繁体   English

Wordpress / PHP - 做一个短代码

[英]Wordpress / PHP - Do a single shortcode

In WP you can filter shortcodes from a string and execute their hooked functions with do_shortcode($string) . 在WP中,您可以从字符串中过滤短代码,并使用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. 这可以通过过滤全局$shortcode_tags do_shortcode() ,运行do_shortcode() ,然后将所有内容恢复原样。

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. 这会将短代码tag_1tag_2应用于评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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