The Right Way to Customize a WordPress Plugin

Video: I presented on this topic at WordCamp Dayton 2014.

It’s very common for developers to customize and extend existing plugins to fit their own needs, which is one of the great advantages of using open-source software. They often do it by making their changes directly to the plugin, though, which creates a security vulnerability and becomes a maintenance hassle.

If you hack a plugin to make your changes, then you create a situation where, in order to upgrade, you have to go through an annoying process of manually diff’ing your version against the canonical one and then syncing them up in both directions. You could make it a little easier by using version control to create a patch against the canonical source, and then refresh it every time an update is released, but it’s still a pain, and developers rarely go to the trouble of doing that, so they miss out on security patches and new features.

Collaborate With the Developer

Before you try anything else, the first thing to do is to send a message to the developer of the plugin and let them know what kinds of changes you need, and offer to work with them to build them into the core plugin. If they’re open to it, then you can send them a patch with your changes. The great thing about this method is that everyone who uses the plugin will benefit from your work, and you’ll have contributed something back to the developer whose work you’ve benefited from.

The developer may not like the idea, though, or it may not line up with their vision for the plugin, so that won’t always work. There are still plenty of options, though.

Extend Without Modifying

The easiest situation is when you just want to add some new functionality to their plugin, but you don’t need to remove or modify anything that the plugin does. In this case, you can simply¬†write a separate plugin that runs alongside the plugin you’re customizing.

You can see a real-world example of this by looking at the code for Google Authenticator – Encourage User Activation.

Using Custom Hooks

If you do need to change or remove functionality from the plugin, though, there’s a much better way that just hacking it, and it’s something you’re already familiar with:¬†hooks.

Just like WordPress provides hooks to allow plugins to customize and extend it, plugins themselves can provide hooks to allow other plugins to customize them.

So, if you’re lucky, the plugin you want to extend was written by a developer who knew enough and cared enough to include custom hooks that will allow you to extend their plugin. If they did, then your job is pretty easy; you just need to write a separate plugin that runs alongside the plugin you’re customizing, and registers callbacks for the custom hooks that the plugin provides.

Unfortunately, most developers haven’t learned to add custom hooks yet, so you won’t always be able to rely on them. But even without them, there are still two more ways that you can customize the plugin that are better than just hacking it.

Overriding Their Callbacks

The plugin you want to customize integrated with WordPress by registering callback functions for Core hooks. So, you can simply remove their callbacks, and replace them with your own. Then inside your callbacks, you can call functions from the other plugin that you need in order to recreate the parts of their functionality that you want, and avoid the parts that you don’t.

If you’d like to study a real-world example of this method, check out Google Authenticator – Per User Prompt.

Sometimes overriding their hooks isn’t ideal, though, because their functions aren’t modular enough to allow you to call individual pieces that you need without also invoking the things you don’t want. Even in that situation, though, there’s still one more method you can fall back on that’s better than just hacking all your changes into the plugin.

Adding Custom Hooks

If it’d be too much work to replace their Core hook callbacks with your own, you could hack the plugin directly, but instead of putting all your changes into it, simply add the custom hooks that you need, and then put the rest of your functionality in a separate plugin.

Then, you can submit a patch to the plugin’s developer and ask them to include the custom hooks you need in the next release of the plugin. If they do, then you can freely upgrade to new releases and your custom plugin will run cleanly alongside theirs.

But even if they don’t, then you can still upgrade by manually patching your custom hooks into each new release. That’s not ideal, but it’s much better than having to patch in all of your changes, or just not upgrade at all.

51 thoughts on “The Right Way to Customize a WordPress Plugin

  1. Thanks Ian , the article is really amazing! I think, I will go for a plugin on the side. I will also create my internal document so I know what I would need to fix in case something goes wrong with an update!

  2. I just went through myself and I had so many changes that I couldn’t just override the actions.

    I ended up creating this tool that allows you to create a child plugin like a child theme. You can make updates to the plugin and still update it without losing your changes.

    I’m posting this here because it relates and hopefully becomes useful to the next person who runs into this issue.

  3. Hello,
    Please I need an urgent support here. How do I move a plugin file in views to any theme and modify it from there? where exactly in the theme will I move the plugin file to?

    I am looking forward to hearing from you soon. Thanks.

  4. Bitcoin And Altcoin Wallets WordPress Plugin Download – WordPress Plugins Download

  5. It would be easy if there was a way to do a search/replace on php files automatically after the plugin update. I wonder why currently there is no plugin for that (or I didn’t found). The only one – “WordPatch” – that does exactly that is abandoned and doesn’t work any longer. Also the patching functionality is closed source using their deactivated api.

  6. The Business Playbook To Adding New Features To Your WordPress Website - Codeable

  7. WordPress Plugin Development Cost: How Much Does It Really Cost? - Codeable

Leave a Reply

Your email address will not be published. Required fields are marked *