WordPress.org

Ready to get started?Download WordPress

Forums

How to replace a function within a plugin? (5 posts)

  1. BunnyBomb
    Member
    Posted 2 years ago #

    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.

  2. BunnyBomb
    Member
    Posted 2 years ago #

    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.

  3. BunnyBomb
    Member
    Posted 2 years ago #

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

    I guess it can't be done then.

  4. postoy
    Member
    Posted 1 year ago #

    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

  5. postoy
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic