当前位置:  开发笔记 > 程序员 > 正文

将过滤器应用于WordPress短代码输出

如何解决《将过滤器应用于WordPress短代码输出》经验,为你挑选了1个好方法。

我想修改WordPress插件的简码输出。

我尝试了以下方法:

$myShortcode = do_shortcode('[print_responsive_thumbnail_slider id="1"]');
echo apply_filters('new_shortcode_filter',$myShortcode);


add_filter('new_shortcode_filter','new_shortcode_filter_callback');

function new_shortcode_filter_callback($myShortcode){
    //modify content right here

    return $modifiedContent;
}

不幸的是,该过滤器未应用于短代码输出。

如果我这样做是为了覆盖简码并修改输出,那么do_shortcode函数将导致无限循环:

function update_shortcode_slider_content() {
    $sliderContent = do_shortcode('[print_responsive_thumbnail_slider id="1"]');;
    //some magic

    return $modifiedSliderContent;
}
add_shortcode('print_responsive_thumbnail_slider', 'update_shortcode_slider_content');

我做错了什么吗,还是有另一种/更好的方式来修改简码的输出?



1> 小智..:

WordPress 4.7引入了一个新的过滤器do_shortcode_tag来实现此目的。https://www.shawnhooper.ca/2017/01/do-shortcode-tag/

推荐阅读
U友50081205_653
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有