Avatar of dfehling
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?
WordPressPHP

Avatar of undefined
Last Comment
dfehling

8/22/2022 - Mon
dfehling

ASKER
Thanks, Julian - sure!

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

<?php

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.
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.
dfehling

ASKER
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!
tags.php
tags-new.php
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ray Paseur

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.
dfehling

ASKER
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.
Julian Hansen

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
dfehling

ASKER
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.
Julian Hansen

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

to
$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.
dfehling

ASKER
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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dfehling

ASKER
I will contact the plugin developer to see if he can add the functionality I need.