WordPress - Overriding a function in a plugin

2020-05-20 09:30发布

问题:

I've been wonder for some time what the best practice is for modifying a plugin created by a WordPress user?

For example there are a couple lines of code I want to change within the Contact Form 7 plugin. The function is called function wpcf7_ajax_json_echo() and is located in:

wp-content > plugins > contact-form-7 > includes > controller.php

Of course I could just change the code right in that file and be done, but then I'm out of luck when I want to update that plugin as my modifications will probably get written over.

I know I should be making this happen via my functions.php file, but I'm not sure how to go about achieving this. Also, that particular function is 100+ lines of code and I'm guessing I don't want to overwrite that whole function, because there is a good chance the author of the plugin may choose to update something in that function in the future.

Does anyone know the cleanest way for me to modify a few lines within that function via my functions.php file?

Thank you!

回答1:

I do not recommend changing the core. However, you are in a bit of a pickle.

You can:

  • Update the function code directly in the plugin
  • Copy the current function code from the plugin and override it in functions.php

In the end, you still run into the same problem - future compatibility.

Either:

  • The update will overwrite your plugin changes.
  • Your function overwrites the plugin changes.

So, as much as I don't want to say it, I'd update the plugin directly. At least then when you upgrade, you'll know pretty quick that your change is missing. Furthermore, it's possible the plugin updates will include your change.



回答2:

You could use SVN if you wanted to maintain forwards compatibility (and your host has SVN available), whilst being able to keep your own changes.

Each plugin that's on the Plugin Directory has to have an SVN repo (that's how the Directory knows if there are updates). Here's the CF7 repo.

Checkout the trunk to your /plugins/ directory inside a folder like /custom-contact-form-7/. Alter the wp-contact-form-7.php file to give it a unique name, and make the changes you want to make to customise it.

To get new updates you can just svn up to get them, and they'll merge with your changes. Though, you may have to clean up merge conflicts sometimes.

Version Control with Subversion is the place everyone starts to learn SVN, if you need it. There's also a Github repo now, if you'd like to fork that.



回答3:

I definitely think you should add your updates to functions.php or to a custom plugin. It's a hassle right now, but MUCH less hassle every time you upgrade the plugin.

You'll always have to reference the changes made in updates no matter what. Even if you're able to extend the functionality without copying this file, you'll have to at least check and make sure your changes still work. And WinDiff/BBEdit's compare will make quick work of that.

So my first suggestion is to override that function.

Second suggestion: I noticed there's some extensions (a, b, c) to this plugin; perhaps you can find out how they made their extensions and use those details to make your own. Well, that's like suggesting you make a new house in order to fix the dripping faucet, but it's an idea.