Contents
Use the WordPress Function (is_plugin_active)
You can use the built in WordPress function is_plugin_active to do the check.
Here is an example where I check if Akismet is activated:
if ( is_plugin_active('akismet/akismet.php') ) {
The catch : you need to know the directory and file name of the plugin. The directory could differ if the plugin was manually installed. Although very unlikely, the file name could change in future plugin updates.
Check If a Class Is Defined (class_exists)
You can check if a specific class exists using the class_exists function. This means you have to go through the plugin code to find a class that is defined, but it should not be too difficult.
Here is an example where I check if the WooCommerce plugin is activated:
if (class_exists('Woocommerce')) {
The catch : the plugin you are checking for needs to define a unique class – some plugins do not. The class name could also change in future plugin updates.
Check If a Named Constant Is Defined
You can check if a specific named constant is defined and then perhaps even do further checks if it is.
In the below example, I am checking if NextGen V2 is activated. I first check if the NEXTGEN_GALLERY_PLUGIN_VERSION constant is defined, and then if it is, I check the value to make sure it is at least 2.0.0.
if ( defined('NEXTGEN_GALLERY_PLUGIN_VERSION') ) { return version_compare(NEXTGEN_GALLERY_PLUGIN_VERSION, '2.0.0') >= 0; }
In most cases, you would only need to check for the existance of the constant, but in this case I had to do some more work for my particular scenario.
The catch : the plugin you are checking for needs to define a constant – some plugins do not. The name of the constant could change in a future plugin updates.
Check If a Function Name Is Defined (function_exists)
If a plugin does not define any classes or constants, you could check for one of it’s unique, prefixed function names using the function_exists function.
Here I am checking if the Hello Dolly plugin is activated:
if ( function_exists( 'hello_dolly_get_lyric' ) ) {
The catch : the plugin needs to use a unique, prefixed function name. The function name could change in future updates.
What I Recommend
I have never used the function_exists check, but realized that it is an option when I wrote this post. I have recently had to check if a specific named constant was defined, as in my NextGen example above. I personally use (and prefer) the class_exists method in most scenarios. I think (or hope) that the class names in a plugin should remain constant and never change. I feel semi-uncomfortable using the is_plugin_activefunction, but that is mostly due to paranoia (probably unfounded, in case a plugin is manually installed by FTP to a different directory). What method do you use?
Revisions
- April 2, 2014 @ 12:41:42 [Current Revision] by PeterLugg
- April 2, 2014 @ 12:41:42 by PeterLugg
No comments yet.