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.

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.

Leave a Reply

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