Senior Full Stack Developer
February 2018
« Jun    


Check If Dependent Plugin Is Active


WordPress has turned into the mostly used CMS over the web and one of the reasons of its popularity is due to the availability of thousands of free/premium plugins and themes.

While developing a WordPress plugin sometime we need to see if the dependent plugin is already install into the system, because our newly created plugin functionality is dependent on the parent plugin so it makes sense to check if the parent plugin is available and is activated.

Register activation hook

WordPress register_activation_hook registers a plugin function to be run when the plugin is activated. So any function attached to this hook will run straightway whenever a plugin is activated.

But before we hook our function to this registration hook it is necessary to understand the process flow.

If you are interested in doing something just after a plugin has been activated it is important to note that the register_activation_hook process performs an instant redirect after it fires. So it is impossible to use add_action() or add_filter() type calls until the redirect has occurred

It is important to note that after register_activation_hook is fired WordPress perform a redirect, so if you try to add_action or add_filter at this point they will not work as you may expect them.

So lets take an example here to clarify this concept. Suppose we are building a plugin which is based on WooCommerce. As our plugin is dependent of WooCommerce so it is necessary to check if WooCommerce is installed before our plugin gets activated.

As you can see from the above example we use add_option to add and save a temporary value whenever our function attached to registration hook is get fired.  Then we use admin_init hook and attached our call back funciton  load_plugin to it. In the end load_plugin function check if Woocommerce class exists, if not we deactivate our plugin and show a nice error message on the admin screen asking the user to install the parent plugin before activating this plugin.

Comments 0
There are currently no comments.