phpwordpresstemplateswordpress-theming

Template Overriding using a customization section


I am trying to override my default templates from customization section, I am using code to do that, but if I am using it I am unable to assign a template to the edit-page page, Can anyone give an idea how both the customization section and edit-page assign template work. I want to set the template when I am creating a page and after assigning it I want to override. Consider I have a blog page, I want to assign it archive.php template and ten want to override It from customization section. There is the particular condition where I want it to work.

 <?php
    /**
     * Adds the Customize page to Select template For Pages
     */

    add_action( 'wp_footer', 'cur_page_template' );
function cur_page_template(){
    var_dump( get_option('current_page_template') );
    var_dump( get_page_template() );
    exit;
}
 function widgetsite_template_override($wp_customize){
    $wp_customize->add_panel( 'template_options', array(
        'title' => __( 'Template Options', 'widgetsite' ),
        'description' => $description, // Include html tags such as <p>.
        'priority' => 160, // Mixed with top-level-section hierarchy.
    ) );

    $wp_customize->add_section('theme_template_override', array(
        'title'    => __('Override  Templates', 'widgetsite'),
        'panel' => 'template_options',
        'description' => '',
        'priority' => 120,
    ));


    $templates = get_page_templates();

    $cats = array();
    $i = 0;
    foreach($templates as $template_name => $template_file){
        //$cats[$template_name] = $template_name;   
        if (strpos($template_file,'layouts') !== false) {
            $cats[$template_file] = $template_name;
        }
    }


    $wp_customize->add_setting('widgetsite_archive_template');
    $wp_customize->add_setting('widgetsite_page_template');
    $wp_customize->add_setting('widgetsite_index_template');
    $wp_customize->add_setting('widgetsite_post_template');
    $wp_customize->add_setting('widgetsite_search_template');

    $wp_customize->add_control( 'widgetsite_archive_template', array(
        'settings' => 'widgetsite_archive_template',
        'label'   => 'Override Archive Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge(array( "archive.php"=>get_option('current_page_template')), $cats)
    ));
    $wp_customize->add_control( 'widgetsite_page_template', array(
        'settings' => 'widgetsite_page_template',
        'label'   => 'Override Page Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge( array( "page.php" =>get_option('current_page_template')), $cats)
    ));
    $wp_customize->add_control( 'widgetsite_index_template', array(
        'settings' => 'widgetsite_index_template',
        'label'   => 'Override Index Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge(array( "index.php"=>get_option('current_page_template')), $cats)
    ));
    $wp_customize->add_control( 'widgetsite_post_template', array(
        'settings' => 'widgetsite_post_template',
        'label'   => 'Override Post Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge(array( "post.php"=>get_option('current_page_template')), $cats)
    ));
    $wp_customize->add_control( 'widgetsite_search_template', array(
        'settings' => 'widgetsite_search_template',
        'label'   => 'Override Search Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge(array( "search.php"=>get_option('current_page_template')), $cats)
    ));

}
add_action('customize_register', 'widgetsite_template_override');

$theme_mode_templates['archive.php'] = get_theme_mod("widgetsite_archive_template");
$theme_mode_templates['page.php'] = get_theme_mod("widgetsite_page_template");
$theme_mode_templates['index.php'] = get_theme_mod("widgetsite_index_template");
$theme_mode_templates['post.php'] = get_theme_mod("widgetsite_post_template");
$theme_mode_templates['search.php'] = get_theme_mod("widgetsite_search_template");


function widgetsite_template_redirect($template){

    global $wp_query;
    global $post;
    $cur= basename($template);
    if(  $cur === 'page.php' && get_theme_mod("widgetsite_page_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_page_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    if(  $cur === 'archive.php' && get_theme_mod("widgetsite_archive_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_archive_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    if(  $cur === 'index.php' && get_theme_mod("widgetsite_index_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_index_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    if(  $cur === 'post.php' && get_theme_mod("widgetsite_post_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_post_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    if(  $cur === 'search.php' && get_theme_mod("widgetsite_search_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_search_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
    }
    return $template;


}
add_filter( 'template_include', 'widgetsite_template_redirect', 99 ); 

Solution

    1. How the choose template box works from post edit screen.

    It is important to remember pages are also posts and all meta relating to posts are stored in the post meta table. Page post types differ slightly from the standard post types as they do not follow the single-postname.php template use function. Instead pages save the template file path in the wp_postmeta database table with a key of _wp_page_template.

    So one option to change this value is to change it after save post.

    function save_template_file( $post_id ) {
    
        if ( 'page' != $post->post_type ) {
            return;
        }
    
        //insert logic here
        $filelocation= 'anywhere.....';
    
        update_post_meta($post_id, '_wp_page_template', $filelocation);
    
    }
    
    add_action('save_post', 'save_template_file', 11 );
    

    Now this is not what you are looking for, but you mentioned you wanted to understand the process, so for pages, wp will reference the template file from post meta and pull this value. So you can change it after saving if it will always follow the same logic (slightly optimized the process). This is the file that shows up in the edit post screen and will always pull the db value unless wp tries to load the template and realizes it does not exist anymore, in which case it reverts to the defaults file in the select box.

    1. The filter template_include is within the function that searches for the correct template for the pages post type (other post types have the filter single_template)

    Your use of include here is incorrect. Don't forget a filter will expect a value returned to work correctly in this case $template.

    So if we want to change the template for pages....

    add_filter('template_include', 'assign_new_template');
    
    function assign_new_template ($template){
    
     //we already have a template name, no need to pull it again..
     $cur= basename($template);
    
     if(  $cur === 'page.php' && get_theme_mod("widgetsite_page_template")){  //note $cur will never be empty!
        $template=  get_template_directory() . '/layouts/' . get_theme_mod("widgetsite_page_template");// assuming this will return correct template...
        //if issues try hardcoding a path to test...
     }
    
    // dont need a else, we will only change the template if our logic is satisfied...
    
    // etc
    
    return $template;
    }
    
    1. Setting the selects

    You are missing the value for default so i propose the following mod as i cant see what your setting in get_option('current_page_template') but if there is a correct filename there replace page.php with it.

    While you are not setting a default value for your select box, your page will render the 1st value of the select if none are marked selected so it should work the same.

    $wp_customize->add_control( 'widgetsite_search_template', array(
        'settings' => 'widgetsite_search_template',
        'label'   => 'Override Search Template:',
        'section'  => 'theme_template_override',
        'type'    => 'select',
        'choices' => array_merge(array("page.php"=>'default'), $cats)
     ));
    

    If you resave all the options like above it should be working (it was for me)!