How to modify a plugin's code from a child theme

Hey,

As I am doing much WordPress development, I seem to have one constant issue; my clients often want to tweak a plugin.

Now, without being from the amazing cowboy state Texas, the easy approach would be to do some cowboy-coding and edit the plugin's code directly. However, this often means that the plugin will not get updated... Oh way! Did I hear somebody say "SECURITY RISK"!

You better believe it!

Now, if the plugin developer is as great as Pippin Williams, then there will be a filter or action ready to take care of any custom changes. But, what if there are no filters or actions available. Is there a smart way to modify a plugin from, for example, a child theme, so I don't have to touch the plugins code itself?

I am most certainly that this thread will become a poplar go-to post for any WP developer!

Sincerely,
Mika

  • Adam Czajczyk

    Hey Mika,

    I hope you're well and thank you for your question!

    You're of course right: the best - and preferable - way of modifying plugins would be to use proper (available) hooks and filters. However, there are many cases in which this isn't possible.

    My "workflow" then would be as follows:

    1. Double-check if there are indeed no hooks/filter available (sometimes these aren't documented)
    2. Check if the plugin code is structural or OOP
    3. In case of structural: in your theme's functions.php (or other included .php file) try to "intercept" return/output of certain functions and alter it (tricky but can be done)
    4. In case of OOP: a lot of classes can be extended (a good way to go):

    http://php.net/manual/en/language.oop5.inheritance.php

    5. In both cases if it comes to shortcodes it is sometimes possible to simply copy a function or class, change its name, tweak the code and then simply use a shortcode with altered name.

    I hope that helps!

    Cheers,
    Adam

  • Adam Czajczyk

    Hey Mika!

    Could you explain point 3 at bit further and if possible, give an example :slight_smile:

    Sure I can, my pleasure :slight_smile: Please note though, that this is not always possible. Two conditions has to be met:

    1. this is structural code, not OOP
    2. the function you're trying to "enhance" isn't "echoing" data but returning it.

    Here's an example (out of my head, not taken from any plugin).

    Let's say that the plugin is a simple online card and there's a function that returns list of product names. This function is designed to be used as a "WordPress template tag".

    function super_store_products() {
    /....
    here's some function code that depends on other plugin's code
    and returns a list of product names in array
    .../
    return $products;
    }

    By design, you'd place this in your page template:

    $my_products = super_store_products();
    foreach ($my_products as $product) {
    echo $my_product;
    }

    Assuming you want to show products images instead of names because plugin doesn't allow this, you could do something like this:

    function super_store_products_images() {
    $my_images=array();
    $my_products = super_store_products();
    foreach ($my_products as $product) {
    $my_images[] =   /... here goes your code that searches db for records containing $product (product name)
    and returns it's ID; having an id you can now fetch product featured image using standard WP functions and display it.../
    
    }
    return $my_images;
    }

    This is of course a "pseudo-code" but I think you shoud get an idea :slight_smile:

    If you have any further questions, don't hesitate to ask! I'll be glad to help!

    Cheers,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.