Get_header do_action('get_header') from template file within plugin

We run around 30 WordPress sites manly which we have acquisition and a few which we have built from the ground up using Sage. Therefore, we have two mediums, one using blade and one using standard php for templates.

We’re developing a plugin which will need to include template files. The idea of the plugin is that it will be plug and play, there should be no editing or copying plugin template file to the theme etc, the plugin should hook into the filter template_include and load the template for the given page from the plugin.

Example

add_filter('template_include', [$this, 'setTemplate']);

public function setTemplate($template)
    {
	    if (is_singular('reviews')) {
            $template = WP_PLUGIN_DIR .'/'. plugin_basename(dirname(__FILE__)) . '/Reviews/single-reviews.php';
	    }

        return $template;
    }

Which works perfectly. There is just one major issue, we’re missing the header and footer, and returned the warning. Which I understand as it’s looking now for a header.php within /views.

Has anyone been any to solve this issue? Is there a solution.

Hey @Jigsawsoul,

I haven’t done a ton of work on this problem for plugins that needed to work with both Sage and non-Sage themes, but off the top of my head I’m not sure off the top of my head that there’s a super clean way to do it that works for both Sage and non-Sage themes if you want to override the entire template (instead of hooking onto the_content or something similar).

You might need to do a little fancy footwork in your plugin to load a Blade template that extends the theme’s base layout (like Sage’s templates do) when Sage is being used, and load a normal .php template when it’s not.

You could also look at Acorn (not sure how ready it is for primetime), which is intended to let you use some of the nice things from Sage, including Blade, in you plugins.

Hope I’m wrong and there’s a simpler solution out there!