PeachPay

Sponsored

WP Mayor is supported by its audience. Our recommendations are based on our experts’ research and hands-on testing. If you purchase through a link on our website, we may earn a commission. Here’s why you can trust us.

How to Check Whether a Plugin is Active

  Introduction
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.
WP Mayor is supported by its audience. Our recommendations are based on our experts’ research and hands-on testing. If you purchase through a link on our website, we may earn a commission. Here’s why you can trust us.
 Table of contents

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 an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.
Get more expert advice like this delivered to your inbox every week.
Join thousands of designers, developers, and marketers who trust our experts to find and test the best tools and services for your website.
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.
Get more expert advice like this delivered to your inbox every week.
Join thousands of designers, developers and marketers who trust our experts to find and test the best tools to build, improve, and optimize their websites.
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.
Join the Conversation

3 Responses

  1. Thanks this is great. Is there a way to check if plugin is only installed but not necessarily activated?

    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
      }
  2. 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.

Leave a Reply

Your email address will not be published.

From web hosting to WooCommerce plugins and backup services, we have put together a collection of exclusive coupons and deals just for you.
WP Mayor_Icon_white

Reader Survey

Supercharge your WooCommerce store!