Post Pic

How to test if a plugin is active

When developing a WordPress theme, you often want to rely on a plugin. For example, I have seen many themes which relied on the Wp-PageNavi plugin for their pagination. But if the end user do not have the plugin installed, the code is broken and you can’t use the theme. Happily, there’s a WP function named is_plugin_active() that you can use to test if a plugin is active.

Nothing complicated: Simply paste the code below where you need to test if a specific plugin is active. Don't forget to update the plugin path on line 2!

<?php
   if (is_plugin_active('plugin-directory/plugin-file.php')) {
      // the plugin is active
   }
?>

Thanks to Cats Who Code for the cool tip!

4 Responses

May 04 2012 15:59

The problem here is that one could rename the plugin folder so you can’t really rely on that.

There are some workarounds such as:

1) storing an option in the DB via the activate hook and remove it on deactivate hook
2) use some filter which is run only when the plugin is active (otherwise no add_filter call)
3) check if a global var from the plugin is available

May 05 2012 12:09

Great tip ! Now I will manage to show an alert on my sites when Maintenance Mode is activated !

May 09 2012 15:07

Works only in Admin. To make it work in template, see Codex http://codex.wordpress.org/Function_Reference/is_plugin_active

Aug 20 2012 07:29

It works perfectly fine with me. Thanks for it!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox