最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Override the core function locate_template

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Feb 12, 2020 at 14:55 FamousWollufFamousWolluf 1391 silver badge6 bronze badges 3
  • 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 Commented 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. – user3135691 Commented Feb 12, 2020 at 16:14
  • I would never change core files – FamousWolluf Commented Feb 13, 2020 at 11:23
Add a comment  | 

2 Answers 2

Reset to default 1

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.

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;
} );
发布评论

评论列表(0)

  1. 暂无评论