I’m building a plugin that adds extra functionality to a main plugin. Ideally in the plugins administration screen, the “activate” link should be disabled and an inline note should be added that tells the user to install and activate the main plugin first before he/she can use the current plugin.
4 comments
Comments are closed.
Thanks for the answers guys. Though both answers set me on the right path, none worked out of the box. So I’m sharing my solutions below.
Method 1 – Using register_activation_hook:
Create the Parent Plugin in plugins/parent-plugin/parent-plugin.php:
Create the Child Plugin in plugins/child-plugin/child-plugin.php:
Notice that I’m not using
deactivate_plugins( $plugin );
as for some reason it does not work. So I used wp_die to cancel the activation redirection and inform the user.Advantage:
Disadvantages:
Method 2 – Using admin_init and admin_notices
Create the Parent Plugin in plugins/parent-plugin/parent-plugin.php:
Create the Child Plugin in plugins/child-plugin/child-plugin.php:
Advantage:
Disadvantage:
As for my question regarding disabling the activate link, I could use:
However, it turned out to be highly impractical as there is NO place to put this code. I could not put it on the parent plugin as the parent plugin should be active for this code to run. Certainly does not belong to child plugin or functions.php. So I’m scrapping this idea.
Both of the suggested solutions have flaws.
Method 1: As mentioned, wp_die() screen will STILL show up when the Parent Plugin and Child Plugin activated at the same time using the checkboxes in the plugins admin screen.
Method 2: In some use-cases it’s not good since ‘admin_init’ is executed way after ‘plugins_loaded’ (https://codex.wordpress.org/Plugin_API/Action_Reference), and after the uninstallation hook (https://codex.wordpress.org/Function_Reference/register_uninstall_hook). So for example, if we do want the add-on to run some code on uninstallation whether the parent plugin is active or not, this approach will NOT work.
Solution:
First of all, we need to append the following code to the end of the parent plugin’s main PHP file:
This will dispatch an event/signal to all subscribers, telling that the core plugin was loaded.
Then, the add-on’s class should look like the following:
Hope it helps 🙂
Try this out, it’s commented, so that should help you understand it.
If this throws out an error, you could also check the ‘option’ of ‘myplugin’ and set it to false or not activated.
I think you need TGM Plugin Activation.