Imagine you are up to write a child- or parent-theme for WordPress and want to have/use theme inheritance. How great it is when you just create a file in the child-theme and the parent will use this instead of his own. Even better when you just have to replace the “get_template_directory” with another function. Read on to know what WordPress can do that no one doesn’t seem to know about.
Do you know some more shortcuts? Then please leave a comment below.
There is theme inheritance in WordPress
Yes, WordPress can. I’ll show you by this structure of the parent theme:
This is what you bought from a professional theme developer or want to build yourself. It is also professional to have a child theme that easily extends the parent. So we want inheritance like this child theme:
The existance of a file can override features from the parent theme.
Isn’t that beautiful? You create the child theme and every file you put there will be taken instead of the parent ones. In this case the parent theme comes with a “[ foo ]” shortcode. But your customer wants it to look different. Just copy the file to your child theme and make the changes – done. No more copying the functions.php, maintaining and fixing things it with every theme update. Simple theme inheritance.
How does WordPress realize theme inheritance?
This is up to the parent theme. WordPress itself offers the “locate_template” function which can be used by a theme. Let me explain this by some code.
Simple change in the parent theme
Most or all themes use “get_template_directory()” and their child themes will use “get_stylesheet_directory()”. Stop that!
If you want to have theme inheritance then let your parent “functions.php” look like this:
<?php // stuff ... require_once locate_template( 'inc/shortcode-foo.php' ); require_once locate_template( 'inc/template-bar.php' ); require_once locate_template( 'inc/widgt-baz.php' );
The “locate_template” function decides where to take the files from. It’s flow is very easy:
- First search in the child-theme / stylesheet path
- Second search in the parent-theme / template path
- Third search in “wp-includes/theme-compat”
So all the magic exists for years now but no one used it. And don’t worry about run-time because the APC cache will recognize what happens here after some calls and fasten things up.
The child theme is able to extend
From now on the “functions.php” of a child theme can be very plain:
<?php require_once( TEMPLATEPATH . '/functions.php'); // other stuff ...
Just one line to activate the capabilities of the parent theme. Everything else will be managed by just creating files.
For instance: The theme had an “widget-baz.php”. Copy this file to your child-theme, modify as your customer wishes and done. Or the “template-bar.php” shall be different. Copy, modify, done.
This is more a change in the community and your own brain than a technical change. It is a well lived inheritance in plenty other Frameworks like Magento, Drupal or Symfony but still not present in the WordPress world. So please start thinking like this and use this simple function for better WordPress Themes. Have fun using this little change!
There is more to know about this and some more snippets. Read the whole story behind this post here: Why this sites lives – the story of locate_template