Mixing and Matching – Custom templates in a WordPress plugin

There doesn’t appear to be any good hook to switch out the Header or Footer templates. Since you need to change both the header and footer of the theme, it might be best to make a page template in your Child Theme. Something like this:

<?PHP
/**
 * Template Name: Plugin XYZ
 * plugin-xyz.php
 */

// Will look for a header-plugin-xyz.php file.
get_header( 'plugin-xyz' );

// Arbitrary action hook for your plugin.
do_action( 'plugin_xyz_before_content' );

    if( have_content() ) {
        
        while( have_posts() ) {
            the_post();
            the_title( '<h1 class="entry-title">', '</h1>' );
            the_content();
        }
        
    }

// Arbitrary action hook for your plugin.
do_action( 'plugin_xyz_after_content' );

// Will look for a footer-plugin-xyz.php file.
get_footer( 'plugin-xyz' );

Creating and calling header-plugin-xyz.php and footer-plugin-xyz.php in your Child Theme will allow you to load in theme-related elements and styles while still making them different than the rest of the website. By making this a page template, you can assign this to a specific page and load in page assets like content or related metadata. You could add hooks throughout this template for your plugin to hook into and modify or inject further templates or content.


The alternative, to keep everything contained in the plugin is to create a template without calling get_header() and get_footer(). You would then need to create your own header and footer elements ( so that wp_head() and wp_footer() get called ). You could create an Endpoint (Make Tutorial) and use load_template hook to call your custom template when loading your custom endpoint.


If you have a Child Theme and this is a one-off plugin then I would suggest the first. If you want to expand this functionality out to other themes then I would suggest not trying to override the theme header and footer as there aren’t good hook to manage these.