Overriding WordPress plugin code in my child theme

dfehling
dfehling used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
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.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Author

Commented:
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
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Most Valuable Expert 2011
Top Expert 2016

Commented:
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.

Author

Commented:
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.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Author

Commented:
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.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Author

Commented:
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.
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Hooks are basically function calls to methods or functions that may or may not be defined by the person using the plugin. It would look something like
if (method_exists(this, "hookname"))

Open in new window

or
if (function_exists("hookname")) 

Open in new window

I don't see anything that is obviously a hook but to be honest this class looks so simple it would be a bit of over-engineering to include them.

As I said before - copy it - rename it, modify it and use the modified version.

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial