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' ); |
I might be wrong, but IIRC from a few years back, another drawback of is_plugin_active() is that it only works in admin – it can’t be used on the front-end – hence the need to do double checking if you want to prefer it over an *_exists() check: https://github.com/GaryJones/Genesis-Admin-Bar-Plus/blob/master/genesis-admin-bar-plus.php#L421
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.
one issue is displqy messages to the user during activation instead of redirecting with wp_die.
Here’s one approach
http://10up.com/blog/wordpress-plug-in-self-deactivation/
That’s a good one.
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.
Nice little analysis. The
function_exists()
technique does look like the best option. I’m going to be doing this very soon so thanks!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.
Correct, that’s why I mentioned using the “plugins_loaded” hook to ensure the check runs after all plugins are loaded.
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' ) ) {}
.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.
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.
Alphabetically.
Could you show me an example of what you’re trying?
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.
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:
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?
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.
Thanks for sharing. but when i use i am getting fatal error.
Fatal error: Call to undefined function is_plugin_active().
You need to load the plugins.php file from WordPress core.
Hi Pippin.
Why not use both methods? For WooCommerce it would be:
// your code
😀
Sincerely,
Mika
Over two years old and this article is still very relevant. Thanks Pippin!
I combined this with the 10up article that Paul mentioned. Now if the Dependent plugin is not active, the child plugin automatically deactivates itself and gives an admin alert to inform the user why it won’t activate.
https://gist.github.com/mathetos/7161f6a88108aaede32a
Glad to hear it was helpful!
Isn’t this a better way to check ?
if ( in_array( ‘easy-digital-downloads/easy-digital-downloads.php’, apply_filters( ‘active_plugins’, get_option( ‘active_plugins’ ) ) ) )
No because there’s no guarantee that the plugin’s folder name will be exactly what you expect it to be.
It seems this is typo but in the condition class_exists( ‘Easy Digital Downloads’ ) you have written class name with spaces instead of underscores ‘Easy_Digital_Downloads’
Thanks for this!
While building a plugin for WooCommerce I figured the best way to make sure WooCommerce is loaded, would be to do the following:
function init_after_woocommerce()
{
if (class_exists(‘WooCommerce’)) {
if (!class_exists(‘MyClass’, false)) {
include_once dirname(__FILE__) . ‘/includes/my-class.php’;
}
}
}
add_action(‘plugins_loaded’, ‘init_after_woocommerce’);
Thanks for this post!
Well, is there any easier way out, like say, a wizard or another plugin or still something else to find out if any plugin is actually in use? I have a few installed and activated as well. But I was looking for some other ways than to deactivate all of them and checking one by one.
Thanks in advance.