Senior Full Stack Developer
November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories


Check If Dependent Plugin Is Active

MubashirMubashir

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.

function my_plugin_activate()
{
    add_option('Activated_Plugin', 'Plugin-Slug');
}

register_activation_hook(__FILE__, 'my_plugin_activate');


function load_plugin()
{

    /* do stuff once right after activation */

    if (is_admin() && get_option('Activated_Plugin') == 'Plugin-Slug') {
        delete_option( 'Activated_Plugin' );

        if (!class_exists('WooCommerce')) {
            add_action('admin_notices', 'self_deactivate_notice');

            //Deactivate our plugin
            deactivate_plugins(plugin_basename(__FILE__));

            if (isset($_GET['activate'])) {
                unset($_GET['activate']);
            }
        }
    }
}

add_action('admin_init', 'load_plugin');

/**
 * Display an error message when parent plugin is missing
 */
function self_deactivate_notice()
{
    ?>
    <div class="notice notice-error">
        Please install and activate woocomemrce plugin before activating this plugin.
    </div>
    <?php
}

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.