Rocket Squirrel Rocket Squirrel
Rocket Squirrel

A global community of coders, developers, and designers

September 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Categories


Checking for a Plugin Dependency on Activation

Dan DulaneyDan Dulaney

Often, WordPress plugins that we create are meant to extend or build on other plugins that already exist. For example, if you search “WooCommerce”, you can find hundreds of other plugins that modify, extend, or otherwise change the capabilities of the base WooCommerce plugin.

Whether these plugins are from the official WooCommerce team or from third parties, they all share one thing in common: without the base WooCommerce plugin installed, they are at best useless and at worst, can cause errors within both your site, and the sites of anyone using your plugin.

WordPress attempts to mitigate this by refusing to activate any plugins that throw a fatal PHP error, but relying on this is far from best practice. We’ll look today at how to let a plugin install fail gracefully, if a dependent plugin is not activated. This particular plugin we are activating requires WooCommerce to be installed and activated.

Ideally, this will take place in a few step process:

We can do this with the register_activation_hook, which runs when a plugin is activated for the first time. The register_activation_hook takes two parameters, the file name for the plugin, and the function to run.

Let’s look at a sample activation hook checking for a dependency.

//function to run on activation
function some_woocommerce_addon_activate() {
  
    if( !class_exists( 'WooCommerce' ) ) {
        deactivate_plugins( plugin_basename( __FILE__ ) );
        wp_die( __( 'Please install and Activate WooCommerce.', 'woocommerce-addon-slug' ), 'Plugin dependency check', array( 'back_link' => true ) );
    }
}

//sets up activation hook
register_activation_hook(__FILE__, 'some_woocommerce_addon_activate');

Line 4 uses the class_exists php function to return false if the class passed as a parameter does not exist. Negating that result with ! allows us to run the code block to gracefully fail only if the code block does not exist. This requires us to know a class unique to the plugin we want to check for. We can also check for function_exists, if there is a unique function you would like to use instead of a class.

Line 5 uses WordPress’s deactivate_plugin function to deactivate this plugin since it is lacking the required dependency, and line 7 sends the user to a custom message explaining why the activation failed, with a link to take them back to the plugin page.

By adding this simple if statement to your register_activation_hook, you can prevent errors, frustrated users, and greatly improve your third party integration.

Math teacher by day, freelance WordPress developer by night through Codeable and word of mouth. I specialize in 3rd party API integrations, WordPress optimization (server and site both), and custom plugin development.

Comments 0
There are currently no comments.