When you write plugins that depend on others, such as extensions for Easy Digital Downloads, you often need to check if the dependent plugin is active and only perform certain actions if it is. There are several ways to check if a plugin is active and I’d like to walk you through some of them.

Using is_plugin_active()

WordPress includes a function called is_plugin_active() that allows you to check if the specified plugin is active.

The function can be used like this:

1
2
3
4
<?php
if( is_plugin_active( 'plugin-folder/main-plugin-file.php' ) ) {
	// Plugin is active
}

The function expects you to pass the exact name of the folder and the file of the plugin (or just the file if the plugin has no folder). If we want to check if Easy Digital Downloads is active, we can use this:

1
2
3
4
<?php
if( is_plugin_active( 'easy-digital-downloads/easy-digital-downloads.php' ) ) {
	// EDD is active
}

This works well, but it has one major draw back. Since it requires the folder/name of the plugin, it depends on those names never changing, which is not something you can depend on, so this function only works if you know with absolute certainly what the name of the plugin’s folder/file is.

For this reason, I prefer a different method.

Using class/function_exists()

If you know of a class or function that is included with the plugin you’re checking for, you can easily use the function_exists() and/or class_exists() functions to determine if the plugin is active.

Here’s a function exists check:

1
2
3
4
<?php
if( function_exists( 'EDD' ) ) {
	// EDD is active
}

Here’s a class exists check:

1
2
3
4
<?php
if( class_exists( 'Easy Digital Downloads' ) ) {
	// EDD is active
}

If the function or the class is registered, meaning the plugin is active, the checks will return true.

These two methods are usually very reliable methods of checking if a plugin is active, though there is a caveat.

Using a function/class_exists check depends on the plugin you’re checking for to be loaded before the plugin performing the check. If the plugin you’re checking for is loaded first, your check will return a false positive.

You can get around this pretty easily, however, by placing your check inside of a function or class tied to the plugins_loaded hook.

Instead of doing this:

1
2
3
4
5
6
7
8
9
10
<?php
/*
 * Plugin Name: My plugin
 */
 
if( class_exists( 'Easy Digital Downloads' ) ) {
 
	// do my plugin stuff here
 
}

You can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
/*
 * Plugin Name: My plugin
 */
 
function my_plugin_init() {
	if( class_exists( 'Easy Digital Downloads' ) ) {
 
		// do my plugin stuff here
 
	}
}
add_action( 'plugins_loaded', 'my_plugin_init' );
    • Pippin

      Yes, I forgot to mention that. If you want to use it on the front end, you have to include plugins.php. It’s noted in the codex.

    • Pippin

      That’s a good one.

  1. Ajay

    Pippin,

    I think the class/function_exists is the better option.

    For a developer, if you require a particular plugin to be active, you’ll most likely know which the main function in the plugin is and so can check for that.

  2. Brad Touesnard

    Nice little analysis. The function_exists() technique does look like the best option. I’m going to be doing this very soon so thanks!

  3. Christopher Ross

    The only drawback to function_exists() is that the plugin may not exist (yet) based on load order for the plugin in question, so it’s valid to use within most actions / hooks (which trigger after the plugin initialization) but not in the plugin file itself.

    I don’t know if there’s a way to set/predict the plugin load order for dependencies.

    • Pippin

      Correct, that’s why I mentioned using the “plugins_loaded” hook to ensure the check runs after all plugins are loaded.

  4. syaiful

    It’s also cool to check a plugin that use a constant, and we can check that plugin active or not by if( defined( 'DEPENDENT_CONS' ) ) {} .

    • Pippin

      Yep, that works well to, though I’d be a little more reluctant to use that method as constants are more likely to be removed than functions or classes.

  5. David Wells

    Is there a specific order in which plugins are loaded? (like alphabetically?)

    I’ve tried every method above but it seems my function or class does not exist even when this is a function called on the plugins_loaded action hook.

    • Pippin

      Alphabetically.

      Could you show me an example of what you’re trying?

    • David Wells

      Here is a gist of the code

      https://gist.github.com/anonymous/7001478

      Its looking for a class that exists in the parent plugin. http://wordpress.org/plugins/cta/

      I’ve tried to use functions as well. We have a function specifically for this too but it just doesn’t work. https://github.com/inboundnow/cta/blob/master/functions/functions.global.php#L680

      I’m thinking it has something to do with the order of plugins loading but I thought that is what the plugins_loaded hook was specially built for.

      Thanks for your help.

    • Pippin

      My guess is that the class only exists in the admin or front end and you are only checking in the other.

      Another way you can do it is to check if this constant is defined:

      define('WP_CTA_CURRENT_VERSION', '1.0.9.7' );
  6. Andrea

    The problem with the order how plugins are loaded shouldn’t be minimised.

    Suppose we have these plugins:
    – plugin-a
    – plugin-b

    plugin-a depends on a global instance of plugin-b. E.g.:
    global $plugin_b;
    $plugin_b->somemethod();

    But $plugin_b is not yet instantiated, because not yet loaded.

    How can we make sure that a plugin-a is loaded AFTER plugin-b?

    • Pippin

      You can’t (not reliably). The advantage of the “plugins_loaded” hook is that all plugins have been loaded, so at that point the order doesn’t matter.

  7. Bhumi

    Thanks for sharing. but when i use i am getting fatal error.

    Fatal error: Call to undefined function is_plugin_active().

    • Pippin

      You need to load the plugins.php file from WordPress core.

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