Link to home
Start Free TrialLog in
Avatar of dfehling

asked on

Overriding WordPress plugin code in my child theme

There is part of a plugin I am using that I need to override in my child theme, namely one of the template files. Is there an easy way to do this?
Avatar of dfehling


Thanks, Julian - sure!

The file in the plugin that I need to override  begins with this:


class Template_Tags extends Template_Block {

    public function __construct( $type = 'my-tags' )
        (and then the rest of the function here)

Open in new window

The file is quite fairly long - around 200 lines - but there are several places I where needed to edit it to get it to work the way I want, so overriding the whole file is ideal, but there may be a simpler way to override the class or the function.

With other plugins, I was able to either copy the file I wanted to override into my child theme with the same folder structure. Or I was able to copy just the function into my child theme functions.php. In this case, neither of those worked.
Avatar of Julian Hansen
In this case, neither of those worked.
Probably because it is a plugin. Unless the author of the plugin has provided specific hooks to change the bits you want to change then you are going to have to change the class file directly.

This obviously has the side effect of potentially loosing your updates in the next update of the plugin.

Without seeing the code and knowing what bits you want to change - not easy to provide advice or say why this particular pluggin is behaving differently from others.
The author has provided hooks, but not for this particular class. Nor do I have any instructions on how to use the hooks.

I have attached the original file and the edited one I want to replace it with. I have several more edits to make in the way the $terms are displayed, but maybe this is enough to get me started. Ultimately, my goal is to change the $term links to not link to the archive.php file , but rather to a customized link that I will build dynamically from the slugs (so far I have removed the links by changing get_the_term_list to get_the_terms and the default_output function).

Thanks in advance!
Maybe step back a bit and ask yourself, "What does this plug-in do?"  Then see if there is a more popular plug-in that can do the basics and may be susceptible of modification for your exact needs.  WordPress has many thousands of contributors and most of the well-understood applications have been investigated and built.  My first criterion for selecting a plug-in is popularity, because that means a lot of sets of eyes have seen it and liked it.
Ray - I'd already explored many plugins for this purpose and this one comes the closest to our needs by far. It's just a matter of changing this one thing that has me a bit baffled.
It's just a matter of changing this one thing that has me a bit baffled.
This is what I would do - but I am not a fan of WP and hunting around trying to find some functionality some other person may have written that approximates my need while checking for security holes etc etc just seems like a big waste of time to me. If licensing permits copy the plugin change it to do what you want - change the plugin name and install it as a new plugin.
Then when / if the plugin is updated repeat the process. But as I said that is what I would do - maybe someone else has a better solution.
I still haven't received any solutions. I have been able to override bits of a plugin before in a child theme, so I know there should be a way to do this. Just not sure what code I need to wrap it in and where it should go.
Just so I understand - the only change you made was line 140 which changed from
$this->add_style( 'width', '60px', 'name' );

Open in new window

$this->add_style( 'width', '100px', 'name' );

Open in new window

If this is a style change - can't you just add to the stylesheet?

Looking at the code - there has been no provision for any sort of hook for that particular functionality.
There may be other parts to this plugin but if this is all it is incredibly simple - I would personally copy it and make my own plugin.
No - I am actually changing more to the file. I changed the way the $terms get built in  private function tags_list( $recipe ), and how they are displayed in the bottom section of the file (removing the default link using archive.php.)  Do you see hooks on how to change those portions and if so, how/where do I use them?

Copying and making my own plugin is not an option. This is a premium plugin I have purchased for my client and it fits 95% of their needs. Making my own plugin makes me personally  responsible for ensuring its compatibility with every WordPress update forever.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I will contact the plugin developer to see if he can add the functionality I need.