One custom theme wrapper for multiple different page templates

i am trying to create a “microsite” within my wordpress site that has a different header and footer then the main website by using one custom base file, lets call it base-microsite-one.php and i want any pages using templates/template-microsite-one-home.php and templates/template-microsite-one-inner.php

the name of the base file doesn’t really matter, i just want two different page templates to load through the same base file.

i am aware that i can create a base file for each page template, but both base files would be identical, so that doesn’t make sense.

in the sage documentation there is a filter example for custom post type base file that would go in functions.php. is there a way i can do something similar with page templates using a filter/hook?

to add to the complexity, i am still using a version of roots 7.* on the site in question.

You could have base-microsite-two.php simply include base-microsite-one.php, a simple PHP include should do it.

i considered that, i am just wondering if there is a way to do it with a filter from extras.php

Sure, there is a filter you can use: https://github.com/roots/sage/blob/master/lib/wrapper.php#L44

that helped. not sure this is the best way to do this, but it allows for all my overrides to be located in one place. here is my solution if anyone else ever needs this:

    function microsite_wrapper($templates) {
        $template_slug = get_page_template_slug(); // Get the current page template slug
        switch ($template_slug) :
            case 'templates/template-microsite-one-inner.php' :
            case 'templates/template-microsite-2015-one-home.php' :
                array_unshift($templates, 'base-microsite-one.php'); // Shift the template to the front of the array
        endswitch;
    
        return $templates; // Return our modified array with the desired base-*.php at the front of the queue
    }
    add_filter('roots/wrap_base', 'microsite_wrapper');
2 Likes