Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress - Overriding a function in a plugin

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!

like image 882
bcoyour Avatar asked Sep 21 '11 16:09

bcoyour


People also ask

How do I override a WordPress plugin?

Copy them into a directory with your theme named /bws-templates. Example: To override the single gallery template, please copy /wp-content/plugins/gallery-plugin/templates/gallery-single-template. php to wp-content/themes/your-theme/bws-templates/gallery-single-template.

How do I override a WordPress function?

Or, you can override an entire function defined in parent theme if it is assigned as a callback to a hook. You remove this filter hook in the child theme functions. php as: add_action( 'init', function() { remove_filter( 'the_content', 'append_thank_you' ); } );

How do I override a plugin code?

For example, if you wanted to override a plugin file called plugin-name. php in a child theme, you would create a file called plugin-name. php in the child theme's directory and copy the contents of the original plugin file into it. The child theme's plugin file would then be used instead of the original plugin file.

How do you override a plugin class?

To implement this, simply write your original class and in braces, define your custom class with complete path. Now when your application runs, restAuthenticationFilter class will be overriden by CustomRestAuthenticationFilter. So this was all about overriding a grails plugin class with a custom one.


1 Answers

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.

like image 180
Jason McCreary Avatar answered Oct 22 '22 16:10

Jason McCreary