How to Check Whether a Plugin is Active

When creating plugins, I sometimes need to check whether another plugin is already installed and active. This might be due to my plugin adding some extra functionality on top of the other plugin. Therefore it doesn't make sense to have my plugin active while the main plugin is not active. The situation is the same when creating add-ons for an existing plugin.
Our Sponsors
Essential plugins for your WooCommerce store
Build unbuildable WordPress websites
Try Kinsta WordPress hosting risk-free for 30 days
The best no-code Instagram feed plugin for WordPress
Automate marketing for WooCommerce, from hi to buy!

When creating plugins, I sometimes need to check whether another plugin is already installed and active. This might be due to my plugin adding some extra functionality on top of the other plugin. Therefore it doesn’t make sense to have my plugin active while the main plugin is not active. The situation is the same when creating add-ons for an existing plugin.

It could also be the case that we need to do some checks for plugins when creating themes. For example, the Genesis theme by StudioPress, checks whether an SEO plugin is installed and activated, and if not, it will load its own SEO functionality. In that case, it doesn’t make sense to duplicate SEO options, so its either the plugin that is used, or the theme’s SEO features.

It’s therefore very useful to be able to create this functionality, let’s learn how to do so.

I recently discovered that WordPress ships with a plugin that is useful in exactly this situation. The function is aptly named is_plugin_active().

In the Admin Area:

[php]
<?php is_plugin_active($plugin) ?>
[/php]

In the front end, in a theme, etc…

[php]
<?php include_once( ABSPATH . ‘wp-admin/includes/plugin.php’ ); ?>
<?php is_plugin_active($plugin) ?>
[/php]

Here’s an example on how we can use this:

[php]
<?php
If (is_plugin_active(‘plugin-directory/plugin-file.php’)) {
//plugin is activated
}
?>
[/php]

This is a very handy plugin, the only issue I find with it is that it won’t work if the user changes the plugin folder name, although this is something quite remote.

I prefer to use a secondary way of checking whether a plugin is installed and active. Take a look at this code:

[php]
add_action( ‘plugins_loaded’, ‘wpmayor_check_other_plugin’ );
function wpmayor_check_other_plugin()
{
if ( class_exists( ‘Other_Plugins_Class’ ) ) {
// do stuff, the other plugin is installed and activated
}

if ( function_exists(‘a_function_in_the_other_plugin’ ) ) {
// do stuff, the other plugin is installed and activated
}

if ( defined( ‘A_CONSTANT_IN_THE_OTHER_PLUGIN’ ) ) {
// do stuff, the other plugin is installed and activated
}
}
[/php]

What’s happening in the code above is that we are checking for a class/function/constant in another plugin. If we find it, then it means that plugin is installed and active. One of those is usually enough, you don’t need to use the three checks together. It’s also important to note the hook that we are using (plugins_loaded). This is the best hook to use as it fires exactly after the plugins having been loaded, and therefore is the right time to perform our checks.

Here’s a live example taken from the Genesis framework, using the exact same concept:

[php]
/**
* Detect plugin by constant, class or function existence.
*
* @since 1.6.0
*
* @param array $plugins Array of array for constants, classes and / or
* functions to check for plugin existence.
* @return boolean True if plugin exists or false if plugin constant, class or
* function not detected.
*/
function genesis_detect_plugin( $plugins ) {

/** Check for classes */
if ( isset( $plugins[‘classes’] ) ) {
foreach ( $plugins[‘classes’] as $name ) {
if ( class_exists( $name ) )
return true;
}
}

/** Check for functions */
if ( isset( $plugins[‘functions’] ) ) {
foreach ( $plugins[‘functions’] as $name ) {
if ( function_exists( $name ) )
return true;
}
}

/** Check for constants */
if ( isset( $plugins[‘constants’] ) ) {
foreach ( $plugins[‘constants’] as $name ) {
if ( defined( $name ) )
return true;
}
}

/** No class, function or constant found to exist */
return false;

}
[/php]

To check for plugins, we just pass them in the form of an array. Here’s how Genesis does it:

[php]
/**
* Detect some SEO Plugin that add constants, classes or functions.
*
* Uses genesis_detect_seo_plugin filter to allow third party manpulation of SEO
* plugin list.
*
* @since 1.6.0
*
* @uses genesis_detect_plugin()
*
* @return boolean True if plugin exists or false if plugin constant, class or function not detected.
*/
function genesis_detect_seo_plugins() {

return (
// Use this filter to adjust plugin tests.
apply_filters(
‘genesis_detect_seo_plugins’,
/** Add to this array to add new plugin checks. */
array(

// Classes to detect.
‘classes’ => array(
‘All_in_One_SEO_Pack’,
‘All_in_One_SEO_Pack_p’,
‘HeadSpace_Plugin’,
‘Platinum_SEO_Pack’,
‘wpSEO’,
),

// Functions to detect.
‘functions’ => array(),

// Constants to detect.
‘constants’ => array( ‘WPSEO_VERSION’, ),
)
)
);

}
[/php]
For more advanced stuff such as requiring other plugins, I can recommend checking out the TGM Plugin Activation library.

TGM Plugin Activation is an incredibly useful PHP library for WordPress that allows you to track and manage plugin/theme dependencies for WordPress. It gives users an easy way to install require and recommend plugins for themes and/or other plugins.

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

Jean Galea
Jean Galea
Jean Galea is a WordPress developer, entrepreneur and padel player. He is the founder of WP Mayor, the plugins WP RSS Aggregator and EDD Bookings, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Consider sharing this post so others can find it:

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on telegram
Share on whatsapp
Share on pocket
Share on email

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services just like this one every week.

Table of Contents

3 Responses

  1. Nice, i usually use class_exist() to detect plugins. still looking for a way to check if the plugin’s widget is active or not. using is_active_widget() seem work with default wp widget but not plugin’s custom widget.

    1. For the benefit of fellow Googlers…

      $path = 'myplugin/myplugin.php';
      
      $all_plugins = get_plugins();
      
      if ( is_plugin_active($path) ) {
          // plugin is installed and active
      } else if ( isset( $all_plugins[$path] ) ) {
          // plugin is installed but not active
      } else {
         // plugin is not installed
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

The Beginner’s Handbook
From an introduction on how WordPress works to our recommendations on products and services.
👋 Hey there! We're Gaby and Mark
Every week we share genuine reviews of WordPress products and services in our newsletter.
Thousands of people read it!
We’d love for you to join.
We’d love for you to join. Here’s what you’ll be getting:

A single weekly email directly to your inbox.