Skip to main content
Robert Michalski

Detecting when WordPress plugin is loaded

Use case #

Detecting if a WordPress plugin is loaded has several benefits.

  1. Prevent site from crashing, either to "White Screen Of Death, WSOD" or in worst case displaying an error message to the visitor.
  2. Increase performance by not running code when a WordPress plugin is disabled or not installed.
  3. Progressive enhancement, enable more advanced features that leverage other installed plugins when they are available, provide basic functionality when they're not available.
  4. Display message to enable required plugins in WordPress backend (WP Admin).

There are probably many more great use cases, but these are the ones I've found useful over the years.

Implementation #

WordPress keeps a record in the database of which plugins are activated, retrieve it directly using get_option('active_plugins') . It returns an array of file-paths to the main php file of each active plugin, relative to (excluding) the WordPress plugin directory, for example woocommerce/woocommerce.php for WooCommerce.

The simplest implementation:

if (in_array('plugin-dir/plugin-file.php', get_option('active_plugins', []))) {
    // TODO: add code dependent on particular plugin here
}

Wrapped in a function for easy reuse:

function YOUR_OWN_PREFIX_is_plugin_active($plugin_path) {
    return in_array($plugin_path, get_option('active_plugins', []), true);
}

if (YOUR_OWN_PREFIX_is_plugin_active('plugin-dir/plugin-file.php')) {
    // TODO: add code dependent on particular plugin here
}

WordPress provides the wp_get_active_and_valid_plugins() function which is safer to use.

It does the following

function YOUR_OWN_PREFIX_is_plugin_active($plugin_path) {
    return in_array($plugin_path, wp_get_active_and_valid_plugins(), true);
}

Example 1: Preventing white screen of death when plugin is not available #

Lets say this code is in your custom theme in the function.php file and the WooCommerce plugin is not installed or has been disabled for some reason. When you load a page containing the code the page goes blank or displays a PHP error message.

$order = wc_get_order(123); // Will result in FATAL PHP error when woocommerce plugin is not available
// Code below will not run

Checking if the plugin is active will prevent the error.

if (YOUR_OWN_PREFIX_is_plugin_active('woocommerce/woocommerce.php')) {
    $order = wc_get_order(123);
    ...
}

Example 2: Adding hooks and filters only when plugins using them are available #

Only adding code when it can actually be used can improve performance and uses less memory.

if (YOUR_OWN_PREFIX_is_plugin_active('woocommerce/woocommerce.php')) {
    function my_hook_handler($a, $b) {
        ...
    }
    add_action('woocommerce_hook', 'my_handler', 10, 2);
    
    function my_filter($value) {
        ... // modify value or do stuff based on value
        return $value;
    }
    add_filter('woocommerce_filter', 'my_filter', 10, 1);
    
    ...
}

Example 3: Displaying a notice to the admin about enabling required plugin(s) #

If a custom plugin or theme uses features from other plugins, display a notice to the WordPress administrator about enabling the required plugins.

    if (!YOUR_OWN_PREFIX_is_plugin_active('required-plugin-name/required-plugin-name.php')) {
        function my_admin_notice() {
            $message = esc_html__('[My plugin name] requires [required plugin] to function. Please install [required plugin].', 'my_text_domain'),
            echo sprintf('<div class="error"><p>%s</p></div>', $message);
        }
        add_action('admin_notices', 'my_admin_notice');
    }