且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WordPress - 重写插件中的函数

更新时间:2023-12-05 23:50:52

我不建议更改核心。然而,你有点腌制。



您可以:


  • 直接在插件中更新函数代码

  • 从插件复制当前的函数代码并在 functions.php 中覆盖它


最后,您仍遇到同样的问题 - 未来兼容性


$ b


  • 更新将覆盖您的插件更改。

  • 您的函数会覆盖插件变化。



所以,尽管我不想这么说,但我会直接更新插件。至少当你升级时,你会很快知道你的改变缺失了。此外,插件更新可能会包含您的更改。


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!

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.