wordpress - Template Overriding using a customization section -


i trying override default templates customization section, using code that, if using unable assign template edit-page page, can give idea how both customization section , edit-page assign template work. want set template when creating page , after assigning want override. consider have blog page, want assign archive.php template , ten want override customization section. there particular condition want work.

 <?php     /**      * adds customize page select template 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 <p>.         'priority' => 160, // mixed 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 $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 never empty!         $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_page_template");// assuming return correct template...         //if issues try hardcoding path test...     }     if(  $cur === 'archive.php' && get_theme_mod("widgetsite_archive_template")){  //note $cur never empty!         $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_archive_template");// assuming return correct template...         //if issues try hardcoding path test...     }     if(  $cur === 'index.php' && get_theme_mod("widgetsite_index_template")){  //note $cur never empty!         $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_index_template");// assuming return correct template...         //if issues try hardcoding path test...     }     if(  $cur === 'post.php' && get_theme_mod("widgetsite_post_template")){  //note $cur never empty!         $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_post_template");// assuming return correct template...         //if issues try hardcoding path test...     }     if(  $cur === 'search.php' && get_theme_mod("widgetsite_search_template")){  //note $cur never empty!         $template=  get_template_directory() . '/' . get_theme_mod("widgetsite_search_template");// assuming return correct template...         //if issues try hardcoding path test...     }     return $template;   } add_filter( 'template_include', 'widgetsite_template_redirect', 99 );  

  1. how choose template box works post edit screen.

it important remember pages posts , meta relating posts stored in post meta table. page post types differ standard post types not follow single-postname.php template use function. instead pages save template file path in wp_postmeta database table key of _wp_page_template.

so 1 option change value change 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 not looking for, mentioned wanted understand process, pages, wp reference template file post meta , pull value. can change after saving if follow same logic (slightly optimized process). file shows in edit post screen , pull db value unless wp tries load template , realizes not exist anymore, in case reverts defaults file in select box.

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

your use of include here incorrect. don't forget filter expect value returned work correctly in case $template.

so if want change template pages....

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

you missing value default propose following mod cant see setting in get_option('current_page_template') if there correct filename there replace page.php it.

while not setting default value select box, page render 1st value of select if none marked selected should work 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 resave options above should working (it me)!


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -