wp-admin » WP DEV: How to check if a WordPress Plugin is Active?

WP DEV: How to check if a WordPress Plugin is Active?

To check if a plugin is active in WordPress, you can use if ( is_plugin_active( ‘plugin-directory/plugin-file.php’ ) ) {

if ( is_plugin_active( 'akismet/akismet.php' ) ) {
    // plugin is active
} else {
    // plugin is inactive
}

You can also use the get_plugins() function to retrieve all the plugins installed on the site and check if a specific plugin is installed:

$plugins = get_plugins();
$path = 'akismet/akismet.php';
if ( array_key_exists( $path, $plugins ) ) {
    // plugin is installed
    if ( is_plugin_active( $path ) ) {
        // plugin is active
    } else {
        // plugin is inactive
    }
} else {
    // plugin is not installed
}

BUT If a plugin’s folder has been renamed, is_plugin_active() and get_plugins() functions will not work as expected because they check the active status based on the folder name and the main plugin file name.


To check if a class is defined in PHP, you can use the class_exists() function, like this:

if ( class_exists( 'Woocommerce' ) ) {
    // class is defined
} else {
    // class is not defined
}

💡 Class names in a WordPress plugin should remain constant and never change.


To check if a named constant is defined, you can use the defined() function, like this:

if ( defined( 'NEXTGEN_GALLERY_PLUGIN_VERSION' ) ) {
    // constant is defined
} else {
    // constant is not defined
}

To check if a function is defined in PHP, you can use the function_exists() function, like this:

if ( function_exists( 'hello_dolly_get_lyric' ) ) {
    // function is defined
} else {
    // function is not defined
}

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended