Single_template filter disables shortcode

sage9

#1

Hello colleagues.

For some reason with Sage 9.0.0, my shortcode does not run when my custom plugin addes a filter to single_template:

add_shortcode('say_hullo', function() {
    die("Hullo, Pooh."); // This never happens (though does in native theme)
});
add_filter('single_template', function ($template) {
    echo "Hi Piglet."; // this prints to the screen
    return;
});

I have experimented with various levels of priority on the add_filter call without success.

Any guesses?


#2

How are you calling the shortcode in your theme?


#3

I’m just calling it in the page’s admin area, tinymce: [say_hullo]


#4

Are you sure this has anything to do with Sage? Have you tried your code on a different theme?


#5

Yes, @benword and thanks for asking. It runs in the Twentyfifteen theme. I had mentioned it in the code comment above (This never happens (though does in native theme)), but should have specified in my text as well.

I think maybe the next step would be to test it with a fresh install of Sage 9.


#6

Same behavior in fresh install of Sage 9.1.

When add_filter('single_template'... (above) is run, “i ran” is printed at the top of the page body and the shortcode does not run (though the text from the database is) and when add_filter('single_template'... is removed the shortcode runs.

I’m not seeing any errors in the php logs nor via XDebug. (I must confess I barely know how to use XDebug yet.)


#7

Is the behavior any different if you return the $template from your single_template filter? i.e.:

add_filter('single_template', function ($template) {
    return $template;
});

#8

Why, yes. It does. Thank you so much. That’s why you are a leader.

I wonder why it works (or at least acts like it does) in some themes without returning the $template.


#9

My guess is that in most themes $template is null so it doesn’t care when you return null, but Sage makes heavy use of those template values.