I would like to override the Wordpress core function locate_template().
I want the function to also search an additinal plugin directory for a template file.
I don't see any filters inside the function, and also no check if the functions exists.
Maybe someone has a better solution for this.

What I'm trying to create is a grandchild theme as a plugin, and I have it worked for regular theme files but not for overriding get_template_part() and locate_template() functions.

For future clearance my website wil look inside the child theme folder for the file if it is not there it will look at the plugin folder and if it is not there it will look inside the parent theme and then the original Wordpress file.
So I build in an extra layer.

  • This can be a very, very tricky topic so beware! I know Sage does it and The Events Calendar also has some logic to use their templates or override via (child) theme. Maybe you can check out how they solved this?
    – kero
    Feb 12, 2020 at 15:53
  • 1
    Just to remind you, one of the core principles of WordPress' philosophy: don't touch the core. What may be helpful is: template_redirect and template_include. Feb 12, 2020 at 16:14
  • I would never change core files Feb 13, 2020 at 11:23

2 Answers 2


locate_template() is not pluggable, and there's no filter. A filter was proposed a decade ago, but it hasn't been implemented. The ticket for that is here, so you can read that discussion to get an idea of why it might not have happened yet.

The template hierarchy filters might be helpful. They will let you filter the template hierarchy for the top-level templates like page.php, date.php, or category.php etc. Similarly, the template_include filter will let you replace the entire template used to render any given page.

But really, WordPress is not designed to support 3 levels of inheritance for templates. The "proper" solution to your problem would be to copy the child theme to a new theme and make your changes to that.

  • Yes I know it is not for 3 level support. I just wish there was because If I want to change something on the website I have to change it at 3 child themes because several files are shared. But maybe on the other hand I could also override most template files with my plugin, witch is already working. And let the locate_template override files over to the regular theme / child theme hierarchy, That way most of my files are managed inside the plugin instead of 3 child themes :) Feb 13, 2020 at 11:19

As @Jacob Peattie explained, you cannot, to this day, override locate_template().

Depending on your need, instead of changing the template search logic, you could override the template in fine with the template_include filter.

For example, if your plugin defined a custom post type my-cpt, you could add this filter:

add_filter( 'template_include', function( $template ) {
    if ( is_singular( 'my-cpt' ) ) {
        return plugin_dir_path() . 'templates/my-cpt.php';

    return $template;
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.