Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply filter to WordPress shortcode output

I would like to modify the shortcode output of a WordPress plugin.

I have tried the following:

$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;
}

Unfortunately the filter isn't applied on the shortcode output.

If I do it like this to override the shortcode and to modifiy the output there will be a infinite loop caused by the do_shortcode function:

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');

Did I do something wrong or is there another/better way to modify the output of a shortcode?

like image 832
Ph4nt0m Avatar asked Dec 07 '15 15:12

Ph4nt0m


1 Answers

WordPress 4.7 introduced a new filter, do_shortcode_tag, to do exactly this. https://www.shawnhooper.ca/2017/01/do-shortcode-tag/

like image 89
Shawn H Avatar answered Oct 20 '22 11:10

Shawn H