Parent Child Theme – Easiest Way!
This solution will work for you if you want your child theme have fewer templates and if any template is missing, it should not break your blog but should go to parent theme and load it.
And a few important things to set,
1) parent theme folder name
2) child theme folder name
3) Template set in child theme style.css must match parent theme folder name
Because child theme name and Template set in child theme style.css are stored in options table.
So I started looking into wordpress core files
wp-includes/template-loader.php
wp-includes/theme.php
These files load templates from active theme on your blog.
What parent theme must have
Your parent theme must have all template files necessary for any typical wordpress theme and parent style.css must have these lines
/*
Theme Name: Parent Theme
Theme URI: Your Blog URL
Description: My Parent Theme
Version: 0.1
Author: Your Name
Author URI: mailto:your_email_goes_here
*/
What child theme must have
style.css must have these lines where my_child_theme must be the name of child theme folder and my_parent_theme must be the name of parent theme folder.
/*
Theme Name: my_child_theme
Theme URI: Your Blog URL
Description: My Child Theme
Version: 0.1
Author: Your Name
Author URI: mailto:your_email_goes_here
Template: my_parent_theme
*/
and screenshot.png and functions.php
functions.php should add_filter to ‘home_template’
add_filter('home_template', 'load_child_home_template');
I am going to take home/index template as an example here. Lets say child theme is having these files. What does this mean? This means you want index.php/home.php loaded from child theme and everything else should come from parent theme.
1) style.css
2) screenshot.png
3) index.php or home.php
4) functions.php
wp-includes/theme.php has
function get_home_template() {
$template = '';
if ( file_exists(TEMPLATEPATH . "/home.php") )
$template = TEMPLATEPATH . "/home.php";
elseif ( file_exists(TEMPLATEPATH . "/index.php") )
$template = TEMPLATEPATH . "/index.php";
return apply_filters('home_template', $template);
}
Wordpress applies filters at the end and we are going to take advantage of it.
child theme functions.php must have this function as well
function load_child_home_template($template) {
if(empty($template)) {
$template = 'index.php'; /* if you have home.php, replace index.php with home.php */
}
return check_child_template($template);
}
function check_child_template($template) {
$template_file_name = basename($template);
$template_option = get_option('template'); /* has 'my_parent_theme' */
$current_theme_option = get_option('current_theme'); /* has 'my_child_theme' */
$theme_root = get_theme_root();
$child_template_root = $theme_root . '/' . $current_theme_option;
$child_template = $child_template_root . '/' . $template_file_name;
$parent_template_root = $theme_root . '/' . $template_option;
$parent_template = $parent_template_root . '/' . $template_file_name;
if(file_exists($child_template)) {
return $child_template; /* if child theme has index.php/home.php, load it */
}
else if(file_exists($parent_template)) { /* if child is missing index.php/home.php, load index.php/home.php from parent */
return $parent_template;
}
return $template;
}
You may want to put some html comments in parent theme index.php/home.php and child theme index.php/home.php to debug which theme is serving home page by looking into source code.
More on themes on Wordpress Codex
Leave a Reply