How To Check If a WordPress Plugin is “Sitewide Active” (or “Network Active”)

I’ve just got a support question for my Countdown WordPress Plugin which was written only for WooCommerce to use their internal schedule functionality. A guy complaint about a problem that the plugin always shows a warning message that tells him that WooCommerce is not installed. It turned out that he had installed my plugin on a multisite WordPress site where WooCommerce was installed site wide but my plugin was not. So the question was: How can I check if a plugin is site wide or network active?

How to check if a plugin is sitewide active
The Countdown plugin showed an error message when WooCommerce was site wide active

How to check if a plugin is active (the wrong way)

I’ve used this snippet to check if a plugin is active:

if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return;

I don’t know where exactly but I’ve found this function in another plugin that was written for WooCommerce. So I thought it was right, but it’s defiantly the wrong way. The reason is that this little snippet does not check if a plugin is site wide (or network) active.

The problem is that on a single blog all active plugins will be written into the wp_options database (“active_plugins” dataset). All network wide active plugins are stored in the wp_site_meta database (in the “active_sitewide_plugins” dataset) instead.

How to check if a plugin is active (the right way)

There is a little function that I’ve come across which does everything we need. The is_plugin_active() function is available since 2.5. I didn’t really know why I haven’t found that function before I’ve started writing the plugin but I think it was because of he fact that this function is not available everywhere. In my case I needed it in the admin_notices (where it seems to be available – in general I believe it’s only available on the admin pages). When you need the function in the init hook you have to include the file that defines the function:

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

Nevertheless, here is how it looks like:

if(is_plugin_active( 'woocommerce/woocommerce.php')) return;

When you take a look into the sourcecode of the is_plugin_active() function you will see that it does nearly the same as the above snippet but also uses another nice function which is called is_plugin_active_for_network().

How to check if a plugin is site wide or network active only

But the above code doesn’t really help if you want to check if the plugin is site wide active only. This can be done with the function is_plugin_active_for_network() instead. Like this:

if( is_plugin_active_for_network( 'woocommerce/woocommerce.php') ) return;

How to check if a plugin is a Network-Only-Plugin

There is another easy function that we can use. It’s called is_network_only_plugin(). It checks for the “Network: true” in the plugins header.

if(is_network_only_plugin('woocommerce/woocommerce.php')) return;

I hope this helps all that guys out there who also write plugins :-)