WordPress - 重写插件中的函数 [英] WordPress - Overriding a function in a plugin

查看:428
本文介绍了WordPress - 重写插件中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想,修改由WordPress用户创建的插件的最佳做法是什么? 例如,有几行代码我想在联系表单7 插件中进行更改。该函数称为函数wpcf7_ajax_json_echo(),位于:

  wp-content>插件> contact-form-7>包括> controller.php 

当然,我可以直接在该文件中更改代码并完成,但是我很幸运,当我想更新该插件,因为我的修改可能会被写入。



我知道我应该通过我的functions.php文件,但我不知道如何去实现这一点。此外,该特定功能是100多行代码,我猜我不想覆盖整个函数,因为插件的作者很可能会选择在将来更新该函数中的某些内容。



有没有人知道最简洁的方式,通过我的functions.php文件修改该函数中的几行?



谢谢!

解决方案

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



您可以:


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

  • 从插件复制当前的函数代码并在 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.

这篇关于WordPress - 重写插件中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆