12

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!

bcoyour
  • 133
  • 1
  • 1
  • 5

3 Answers3

7

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.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • 1
    How would one override a function in functions.php? –  Dec 28 '17 at 10:25
  • If a plugin's function is defined like this: `if ( ! function_exists( 'plugin_function' ) ) { function plugin_function() {...} }` .. you can just define it first in your child theme's functions.php and make it do whatever you want, or leave it blank to completely remove its functionality. By the time the plugin tries to define it, it already exists so it skips over it, leaving yours. If it doesn't check for an existing definition, you may have to edit the plugin file or face a fatal error. – AFOC Apr 17 '20 at 23:16
6

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.

Shane
  • 1,015
  • 2
  • 12
  • 31
  • Thank you, that's a good idea. Granted it still does require some manual work each time. Though, that's the way it goes. – bcoyour Sep 21 '11 at 18:13
  • 2
    +1 for advocating version control. However, this is a lot of overhead: checkout the repo, make changes in your working copy, export to current project, manage working copy. You've essentially *forked* the project for a few lines of code changes. – Jason McCreary Sep 21 '11 at 18:21
  • Awesome idea! **Forking** the project seems like the way to go. This is also doable with **Git**, for any StackOverflow wanderers who are wondering! – Jordan Thornquest Jul 08 '13 at 14:09
0

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.

Michael Greisman
  • 410
  • 4
  • 10