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

Ph4*_*t0m 3 wordpress shortcode

我想修改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;
}
Run Code Online (Sandbox Code Playgroud)

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

如果我这样做是为了覆盖简码并修改输出,那么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');
Run Code Online (Sandbox Code Playgroud)

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

小智 6

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