I am working on a site for a client who has a theme built for them by a different developer. They have asked me to make some changes to the site, and I'd like to put all of my changes into a plugin as a courtesy to the original theme developer. So far this is simple with functions and CSS. Now I need to change a theme file. Essentially I'd like to replace one theme file with a different one that I've edited heavily.

In the theme, the file I'd like to replace is called with this:

get_template_part( 'partials/post', 'sidebar' );

What I'd like to do is to somehow write a function in my plugin that catches the above statement and redirects that template request to a file that resides in my plugin directory.

I have absolutely no idea how to do this, and searching has so far turned up very little help. Is this possible?

  • 1
    As @Tom Malone answered this is a perfect situation to use child theme , the reason why original developer used get_template_part( ) function is to give child thenes a priority. If not he just have used require( ) – bravokeyl May 31 '16 at 3:36

TL;DR: Use a child theme instead :)

If we dig into the code for get_template_part(), there's an action there - get_template_part_{$slug} - which we could hook into.

Digging a bit further, get_template_part() calls locate_template(), which calls load_template(). Unfortunately, there's nothing in either of these functions that seems to be pluggable, so we can't hook in and change anything (with the exception of being able to set variables through the call to extract() in load_template().... but we can't override the template file being called so it's not very useful anyway!)

So, it looks like your only option to catch this is by running code on the get_template_part_partials/post action. The problem is, this won't allow you to filter the template called - it'll just allow you to run some code when the template is called. If all you want to do is add some output above the templated code then this will suit you perfectly, but I doubt that's all you want to do!

The way around all this is to use a child theme.

You said you wanted to put your code into a plugin in order to respect the original theme developer. Firstly, that respect is awesome - many developers wouldn't care less so I commend you for that!

However, is a plugin the best choice? Plugins are generally used to add modular functionality to a site, not to change the look and feel of it. That's the domain of themes.

You can in fact create a child theme which will let you modify the theme the site uses without making any changes to the parent theme. You should be able to apply all the changes you've already made here, plus you have the bonus that get_template_part() will automatically use a file within your child theme if it exists, before falling back to the parent theme.

| improve this answer | |
  • 2
    This is what I had decided as well. I was trying to keep all of my edits in one place, in the plugin, but that's just overcomplicating things when the child theme is essentially specifically built to do what I was looking to do. Thank you. – WilliamAlexander May 31 '16 at 10:32
  • @WilliamAlexander Pleasure. I learnt that get_template_part isn't pluggable in the process of looking into this for you, so I enjoyed learning something too! The child theme will let you keep all your edits in one place anyway. You could also make use of get_template_part yourself to further split changes out to keep portions of changes together, if you wanted to. – Tim Malone May 31 '16 at 10:35

first of all you find directory of your partial-sidebar.php directory. when you find then Get_template_directory_uri(). then "../ " this means one folder back if your partial-sidebar.php is backed folder of plugin then use as "../Get_template_directory_uri()/wp-content/" as well as so on

| improve this answer | |
  • Thank you for your answer, however this doesn't answer the question. – Tim Malone May 31 '16 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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