Last active
September 30, 2015 16:53
-
-
Save PrafullaKumarSahu/dbf520236f7f0dfb5922 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function widgetsite_template_manage($wp_customize){ | |
$wp_customize->add_section('theme_template_manage', array( | |
'title' => __('Layouts', 'widgetsite'), | |
'description' => '', | |
'priority' => 120, | |
)); | |
$templates = get_page_templates(); | |
$cats = array(); | |
$i = 0; | |
foreach($templates as $template_name => $template_file){ | |
//$cats[$template_name] = $template_name; | |
$cats[$template_file] = $template_name; | |
} | |
$wp_customize->add_setting('widgetsite_new_template', array('default', "abcd")); | |
$wp_customize->add_control( 'widgetsite_new_template', array( | |
'settings' => 'widgetsite_new_template', | |
'label' => 'Select Template:', | |
'section' => 'theme_template_manage', | |
'type' => 'select', | |
'choices' => array_merge(array("default"=>get_option('current_page_template')), $cats) | |
)); | |
} | |
add_action('customize_register', 'widgetsite_template_manage'); | |
/** | |
* Adds select box to override default templates | |
**/ | |
function widgetsite_template_override($wp_customize){ | |
$wp_customize->add_panel( 'template_options', array( | |
'title' => __( 'Template Options' ), | |
'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; | |
$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("default"=>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("default"=>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("default"=>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("default"=>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("default"=>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; | |
if( (get_page_template_slug( $post->ID ) == 'page.php' || get_page_template_slug( $post->ID ) == '' ) && get_theme_mod("widgetsite_page_template")){ | |
include( get_template_directory() . '/layouts/' . get_theme_mod("widgetsite_page_template") ); | |
exit; | |
} | |
else{ | |
include( get_template_directory() . '/page.php' ); | |
exit; | |
} | |
if( (get_page_template_slug( $post->ID ) == 'archive.php' || get_page_template_slug( $post->ID ) == '' ) && get_theme_mod("widgetsite_archive_template")){ | |
include( get_template_directory() . '/layouts/' . get_theme_mod("widgetsite_archive_template") ); | |
exit; | |
} | |
else{ | |
include( get_template_directory() . '/archive.php' ); | |
exit; | |
} | |
if( (get_page_template_slug( $post->ID ) == 'index.php' || get_page_template_slug( $post->ID ) == '' ) && get_theme_mod("widgetsite_index_template")){ | |
include( get_template_directory() . '/layouts/' . get_theme_mod("widgetsite_index_template") ); | |
exit; | |
} | |
else{ | |
include( get_template_directory() . '/index.php' ); | |
exit; | |
} | |
if( (get_page_template_slug( $post->ID ) == 'post.php' || get_page_template_slug( $post->ID ) == '' ) && get_theme_mod("widgetsite_post_template")){ | |
include( get_template_directory() . '/layouts/' . get_theme_mod("widgetsite_post_template") ); | |
exit; | |
} | |
else{ | |
include( get_template_directory() . '/post.php' ); | |
exit; | |
} | |
if( (get_page_template_slug( $post->ID ) == 'search.php' || get_page_template_slug( $post->ID ) == '' ) && get_theme_mod("widgetsite_search_template")){ | |
include( get_template_directory() . '/layouts/' . get_theme_mod("widgetsite_search_template") ); | |
exit; | |
} | |
else{ | |
include( get_template_directory() . '/search.php' ); | |
exit; | |
} | |
} | |
add_filter( 'template_include', 'widgetsite_template_redirect', 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.Can anyone help ???