How to Check If Plugin is Active In WordPress [ 3 WAYS ]

check if a plugin is active in WordPressAs a WordPress developer one of the most common checks in your code when developing a plugin is the status of the plugin activation.There are several reasons why you should check if a plugin is active. To mention a few, you can check if a dependent plugin is active or a similar plugin that does the same thing.

In either case you will need to add the check in your plugin base file to check if the targeted plugin is active. A classic example if when you are developing a WooCommerce plugin that extends WooCommerce classes.

For example when you are developing WooCommerce payment gateway plugin, you need to check if WooCommerce is active immediately after your plugin is activated or preferably during the plugin activation hook.

Check If A WordPress Plugin Is Active

Luckily, WordPress core has a ready to use method or function that checks if a certain plugin is active and returns a true or false.

is_plugin_active WordPress

This function returns a boolean - true or false and you can easily incorporate it in your code especially during the activation hook.

If the plugin you are checking is not active, you can display a header notice for the user to install the plugin.

You can also apply the same function when you want the user to deactivate another plugin that is likely to conflict with the functionality of your plugin.

Function is_plugin_active() Quick Review

Let us quickly look at the function is_plugin_active() to learn more on how to use it effectively in your project.

is_plugin_active( string $plugin )

This function takes a string parameter that is the representation of the path to the plugin relative path in the plugins directory.

function is_plugin_active( $plugin ) {
    return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
}

For example WooCommerce is path is woocommerce/woocommerce.php as shown on the
image below :
WooCommerce Basefile File Path

The function is_plugin_active() can be used to check if plugin is active in the backend using the following code snippet

<?php
/**
* Detect plugin. For use in Admin area only.
*/
if ( is_plugin_active( 'plugin-directory/plugin-file.php' ) ) {
//plugin is activated
}

For example if you want to check if WooCommerce is active you can check using the following code :

<?php
/**
* Check if WooCommerce Plugin is active in the admin area
*/
if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
//WooCommerce plugin is activated
}

Plugin Basefile & Base Directory

As you can see I have used the WooCommerce plugin basename and the folder as I mentioned an illustrated using the image above.
The basename of a plugin is the file name for a single file plugin that is the main file and sometimes it matches with the directory / folder but not all cases – some plugins are different.

Check if Plugin is Active in Multisite

For WordPress multisite there is a function that is designed to help developers check if the plugin is active in the multisite network. The function is :

is_plugin_active_for_network( string $plugin )

This function works just like the is_plugin_active( string $plugin ) function and can be applied the same way to determine if the plugin is active across the WordPress multisite network.

<?php
function is_plugin_active_for_network( $plugin ) {
    if ( ! is_multisite() ) {
        return false;
    }
 
    $plugins = get_site_option( 'active_sitewide_plugins' );
    if ( isset( $plugins[ $plugin ] ) ) {
        return true;
    }
 
    return false;
}

Other Ways to Check if Plugin is Active

There are other ways you can employ to check if a plugin is active. You can check using the native php functions class_exists or function_exists. These methods checks if the class of the plugin or the function exists respectively.

Using function_exists() to Check if Plugin is Active

If you know of the function or method of a plugin you can check using the function_exists method as in the snippet below:

<?php
if( function_exists( 'plugin_function' ) ) {
// Plugin is active
}

Using class_exists() to Check if Plugin is Active

If you know of the main class of a plugin you can check using the class_exists method as in the snippet below:

<?php
if( class_exists( 'plugin_class' ) ) {
// Plugin is active
}

You can hook either of these methods to check if plugin is active on the plugins_loaded hook as in the code snippet below :

<?php
/*
* Test if a plugin is Active
*/

function njengah_is_plugin_name_active() {
if( class_exists( 'Plugin_Class' ) ) {

// Its active so do the logic here

}
}
add_action( 'plugins_loaded', 'njengah_is_plugin_name_active' );

Conclusion

In this post we have highlighted the various ways to check if a plugin is active when you are developing another plugin or theme. These are useful methods that you can apply across all your WordPress development tasks.

Similar Articles

Comments are closed.