WordPress - Overriding a function in a plugin

后端 未结 3 777
醉酒成梦
醉酒成梦 2021-02-02 17:55

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 chan

相关标签:
3条回答
  • 2021-02-02 18:36

    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.

    0 讨论(0)
  • 2021-02-02 18:46

    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.

    0 讨论(0)
  • 2021-02-02 18:53

    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.

    0 讨论(0)
提交回复
热议问题