Load different template file when condition met?

Can you perform the logic on the template_include filter? Else, you could set a global/constant and use that in template_include to serve up the appropriate template.

The template_include filter filers the path to the template file to be included. To avoid errors you should check if the template exists – locate_template does this for theme/child-theme files.

add_filter("template_include", "sc_redirect_properties_to_registration");
function sc_redirect_properties_to_registration( $template )
{
    if( is_user_logged_in() && ! is_page("the-properties") )
        return $template;

    //You could include templates from plugins
    //$template = plugin_dir_path(__FILE__).'templates/plugin-template.php';

    //Best to check the template exists. With **theme/child-theme** templates
    // you can do this with locate_template.
    return locate_template('template-registration.php');
}

Leave a Comment