When working on various solutions that are based on WordPress, you will most certainly need to extend some part of a plugin. But what if that plugin is not really extensible? How should we solve that?

In a previous article, I have already talked about 5 Ways to Make your WordPress Plugin Really Extensible. While working on a project, I had to make another plugin extensible through my own solution. Let’s see how we can extend non extensible plugins.

Recreate it?

That could cross your mind. You could always copy the entire plugin, change its name and then extend what you want. Maybe even extend it?

I would not solve the extensibility issue in this way. Why? What happens if the original plugin gets updates? You will have to update everything and then implement the extensibility once more (if the plugin did not add that on its own).

If you are working on your own solution, you might approach it that way but I don’t recommend it. If you are working for a client, then this certainly is not the correct way.

Contribute to the Plugin

If you follow the WordPress community or are involved in any way with it, then you must know something about contributing. That is one of the ways you could solve the extensibility. Contributing it on the plugin can help.

But, if your project is time sensitive you can’t wait for the plugin author(s) to look at your contribution and decide to accept it.

Also, there can be plugins that do not accept contributions or don’t have any information on how to contribute.

In conclusion, this is a viable solution but if you need something right now, then you’ll have to approach it differently.

How to contribute?

Each plugin author(s) will have their own contribution rules. In general, if a plugin is hosted on a GitHub, then there will likely be a way to contribute to it. Let’s take an example of Easy Digital Downloads. The main plugin is free and it is also hosted on GitHub: https://github.com/easydigitaldownloads/easy-digital-downloads.

They also accept contributions and there are also contribution guidelines which you are welcome to read if you are to contribute to EDD.

Extend it within your Solution

This is something that might be of help to you. When you’re working with various plugins, especially premium plugins (which are not easy to contribute to), you will probably have to extend it within your solution.

I will show you one particular situation in which I was and how I have solved it. The idea about this article also came from that.

The Problem

I had to create a points system for a project. For that, I went with Points and Rewards add-on for EDD. The plugin has a messaging system built in to notify the user if a product will award him with points. On the checkout screen, there will be several messages. One of them will ask them to apply the points on it and get a discount.

If they choose to apply the points, then there will be another message. That message will give them the option to remove the points from that order. My project would not allow that since the points should be automatically applied without an option to remove them.

No Extensibility

I have looked for a filter to remove that as an option. That would be an easy fix for it. But there were no filters applied on it. Also, if a message was empty (since you can control what each message will say), the HTML will still be rendered and you will be left with an empty box.

I had to change that somehow. So, how to approach it.

The Solution

First, I went looking where the messages are rendered. The messages are formed and displayed in a class EDD_Points_Renderer. Most of the messages are contained in several methods that are hooked on some EDD regular actions.

The object of EDD_Points_Renderer is also saved in a global variable $edd_points_render. So, what I have done? I have copied that class, made my own and change the methods. I have added some if-else checks to render or not the messages.

What could I have also done? Just extend the same class and redefine the methods.

Then I had to remove the previously hooked methods from the original class and then add hook my own methods in the same way. Here is a piece of the code:

So, what have I done here? I retrieved the global renderer variable and removed its methods from the hooks. After that, I have instantiated my enhanced renderer class and hooked its methods in the same way (and same order).

Conclusion

WordPress is great and the plugins are really awesome. But some of the plugins are not implementing the Plugin API as much as they should. Maybe because they don’t see a reason behind it? But you never know when another developer will have to extend something or change. When writing your plugins, be sure to look at the WordPress Plugin API and consider applying some of it to your returned values.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

2 Comments

  1. Did you submit your changes to EDD?
    Could you have used CSS to hide the generated HTML? Your code was creating an empty message, maybe you could add a CSS class to the page body or somewhere else and then use that to hide the html. (As the add-on is not free I could not look at its code to check this idea).

    Aside: As EDD has so many hooks and filters, it is surprising that this add-on doesn’t have them.

    Reply

    1. Hi Damien,

      I could use CSS, but then I would hide more than 1 message and I did not want that since some of the messages are actually useful. But they all used the same class so CSS would not be of help.

      I was also surprised by the lack of hooks but then I have checked and saw that the “Points & Rewards” add-on as not built by them but by another company.

      Reply

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.