locate_template – Inheritance for WordPress themes

One day at a WordCamp someone ask why proper theme inheritance is not possible in WordPress. It was my first WordCamp and I was a bit flashed because days before I found out that it can – very clean and simple. This is the story how neat and simple theme inheritance will be revived. Read the more up to date and famous one afterwards!
WordPress 4.7: Custom bulk actions, Theme inheritance and more
Open in new tab for later
“It is could be so easy but… WordPress” was the phrase and a bit of laughter. So I immediately told where I found it in the core, why I got there and that WordPress is just an unread thing with plenty of solutions for all of that.

I knew he was wrong because I blogged about it in German already. Nowadays while some theme developer do really great themes I feel the need to write again in englishImagine you are up to write a child- or parent-theme for WordPress and want to have/use inheritance. It is great to just create a file in the child-theme and the parent will use this instead of his own. Even better when the effort is only the replacement of “get_template_directory” with another function. Read on to know what WordPress can do that no one doesn’t seem to know about.

There is theme inheritance in WordPress

Yes, WordPress can. I’ll show you by this structure of the parent theme:

  • style.css
  • functions.php
  • inc/
    • shortcode-foo.php
    • template-bar.php
    • widget-baz.php

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:

  • style.css
  • inc/
    • shortcode-foo.php

The existence of a file can override the parent theme.

Isn’t that beautiful? You create the child theme and every file you add will be taken instead of the parent. 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 – nothing more nothing less.

How does WordPress offer 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! Please. If you want to have theme inheritance then let your parent “functions.php” look like this:


// 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 it searches in the child-theme / stylesheet path
  • Secondly it searches in the parent-theme / template path
  • Thirdly it searches 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 the parent

From now on the “functions.php” of a child theme can be very plain:


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 mind than a technical change. It is a well lived inheritance in plenty other Frameworks like Magento, Drupal or Symfony but still not lived in the WordPress world. So please start thinking like this and use the simple “locate_template” for better WordPress Themes. Have fun using this little change!

Leave a Reply

Your email address will not be published. Required fields are marked *