When we develop an extension plugin, we may use some functions that are available only in core plugin. For example, recently I’ve developed Easy Digital Downloads – Sales Number extension plugin that use a function from core EDD plugin.
When the core plugin is not active, this plugin won’t work because you know, I use a function and hooks from the core plugin. In order to save user from confusion, we need to do an extra check to make sure our extension plugin can be activated only if the core plugin is active.
Take a look at the snippet below, it’s taken from EDD – Sales Number extension plugin.
On line 3, we check if core plugin Easy Digital Downloads is not active to make sure the function will run only if core EDD plugin doesn’t exist. We use class
Easy_Digital_Downloads from the core plugin to do the check. If the core plugin that you develop the extension plugin for uses procedural programming/doesn’t use OOP, you can do the check using
function_exists() to check unique function name available in the core plugin.
On line 5, we check if our extension plugin is active. When we activate a plugin, its status will be active directly. So to make sure we will deactivate an active plugin, we add this conditional.
On line 7, we deactivate the extension plugin. So whenever user activates the extension plugin without active core plugin, it will be deactivated right away.
On line 9, we remove success notice that is displayed when a plugin is activated successfully. We already deactivated the plugin on line 7, so the notice will confuse user if it’s displayed.
On line 11, we add our own custom notice to indicate that this extension plugin requires the core plugin so that user will know what to do next.
This whole function is hooked to
admin_init action hook. Whenever user deactivates the core plugin, our extension plugin will also be deactivated automatically.
Then, here is our own custom notice function:
The function is pretty straightforward. We just need to output HTML along with our error message.
That’s it! Quite easy and straightforward, isn’t it? If you have any question, comment, or anything, don’t hesitate to put your comment below!