• Hello,

    I would like to know if it’s possible to override a function declared within a plugin, by inserting code into my child theme’s functions.php file.

    Based on my Google research, I’ve found I can either:
    1. Edit the plugin files and risk changes being overwritten later
    2. Copy the entire plugin into my functions php, and miss out on future updates from the plugin author

    Is there another way to do this?

    I’ve tried placing my edited function into my child theme’s functions.php file, surrounding it by this (example only):

    if( !function_exists('the_plugin_function_name;)) {
         function my_better_function( 'code goes here' );
    }

    I’d appreciate some advice here. I’m only a beginner with php.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter BunnyBomb

    (@bunnybomb)

    Since I know folks prefer specifics…

    Here is the modified function I would like to use, to replace the one contained in the plugin files. http://pastebin.com/RXmR4H3J

    I needed to make some cosmetic changes from line 85 to 97, then a few more from line 139 to 156. As you can see from the code I’ve posted, I’ve attempted to surrounded it with function_exists(), however it doesn’t work when I paste into my child theme’s functions.php file.

    Would appreciate some advice here.

    Thread Starter BunnyBomb

    (@bunnybomb)

    So nobody knows a way to override a plugin function without editing it directly?

    I guess it can’t be done then.

    I presume you are trying to replace a plugin’s function within a class. It’s called monkey patching and your limitations are with PHP itself. More here:

    http://stackoverflow.com/questions/137006/php-redefine-class-methods-or-class

    Apologies for the dated information. You should be able to achieve this with PHP 5.3+

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to replace a function within a plugin?’ is closed to new replies.