Home > OS >  Run shortcode before add_filter();
Run shortcode before add_filter();

Time:04-28

So I have the shortcode [my_example_shortcode] on my WordPress site.

add_shortcode( 'my_example_shortcode', 'shortcode_for_project' );

I want to prioritise the shortcode before add_filter('the_content', 'my_example');

If the shortcode is not found on the page then I want the the code above to be applied.

I have already tried:

if (shortcode_for_project) {
  echo do_shortcode('[my_example_shortcode]');
}
else
{
add_filter('the_content', 'my_example');
}

CodePudding user response:

If I understand the problem here is one way you can do this:

// functions.php
// hook into the_content filter
// save shortcode output to buffer
// return new content with shortcode output prepended to content
add_filter("the_content", function($content) {
    ob_start();
    
    echo do_shortcode('[my_example_shortcode]');

    $shortcode_output = ob_get_clean();

    return $shortcode_output . $content;
});

CodePudding user response:

Nope! Sorry, it didn't work @mikerojas :/ Let say I have a page:

Business Intelligence

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sem magna, maximus eget odio vel, dictum ultrices nunc.

Then I want add_filter('the_content', 'my_example'); to be enabled

But if

Business Intelligence

[my_example_shortcode]

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sem magna, maximus eget odio vel, dictum ultrices nunc.

Then I want add_filter('the_content', 'my_example'); to be disabled

You see? :)

  • Related