Run shortcode before add_filter();

558 Views Asked by At

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

Let's 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

1

There are 1 best solutions below

3
On

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

// functions.php

add_filter("the_content", function($content) {
    if (shortcode_exists('my_example_shortcode')) {
        ob_start();
        
        echo do_shortcode('[my_example_shortcode]');

        return ob_get_clean();
    }

    return my_example($content);
});