How to check if WooCommerce is activated on WP multisite

Typically the plugins I write are not standalone but require other plugins such as WooCommerce to be enabled to work correctly. So before I run my plugin, I need to check if the plugin I depend on is activated. In a multisite environment this gets a bit more complicated.

If my own plugin is networkwide activated, then I require for the plugin I depend on to also be network activated. In theory it is possible for a plugin to be locally activated on all sites within the network, but if that’s the case then my assumption is that the admin will continue to follow the same logic and activate my plugin also locally.

If my plugin is locally activated, then the plugin I require can either be networkwide activated or locally. This gives the admin the choice to run the plugin I depend on networkwide, but my plugin only locally on a specific site.

To test if the plugin I need is activated I can use the following two WP functions:

is_plugin_active_for_network() does exactly this. It checks if the plugin is activated networkwide and returns TRUE if that’s the case. The WP codex shows in the source code how that’s done:

function is_plugin_active_for_network( $plugin ) {
    if ( ! is_multisite() ) {
        return false;
    }
 
    $plugins = get_site_option( 'active_sitewide_plugins' );
    if ( isset( $plugins[ $plugin ] ) ) {
        return true;
    }
 
    return false;
}

is_plugin_active() confused me at first. My assumption was that this function checks if a plugin is locally activated but that’s not the case. This function multitasks and checks if a plugin is either networkwide or locally activated. Either will return TRUE. The WP codex source code shows how this works:

function is_plugin_active( $plugin ) {
    return in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) || is_plugin_active_for_network( $plugin );
}

Checking if Woo is enabled

Putting this in practice, this is the code I use in my plugin to check if WooCommerce is enabled:

// Makes sure the plugin is defined before trying to use it
 $need = false;
		
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
  require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
	
// multisite 
if ( is_multisite() ) {
  // this plugin is network activated - Woo must be network activated 
  if ( is_plugin_active_for_network( plugin_basename(__FILE__) ) ) {
    $need = is_plugin_active_for_network('woocommerce/woocommerce.php') ? false : true; 
  // this plugin is locally activated - Woo can be network or locally activated 
  } else {
    $need = is_plugin_active( 'woocommerce/woocommerce.php')  ? false : true; 	
  }
// this plugin runs on a single site	
} else {
  $need =  is_plugin_active( 'woocommerce/woocommerce.php') ? false : true; 	
}
	
if ($need === true) {
  add_action( 'admin_notices', 'need_woocommerce' );
  return; 
}

And the error message function like this:

function need_woocommerce() {
  $error = sprintf( __( 'The plugin requires WooCommerce. Please install and active the  %sWooCommerce%s plugin. ' , 'foo' ), '<a href="http://wordpress.org/extend/plugins/woocommerce/">', '</a>' );
  $message = '<div class="error"><p>' . $error . '</p></div>';
  echo $message;
}

Database structure

If like me you like to head right over to the DB to see how Woo stores this data, this is the structure for storing networkwide and locally activated plugins.

Networkwide plugins are stored in the table “prefix_sitemeta” with meta_key “active_sitewide_plugins”, and the site_id of the primary site (in my case 1).

Image shows database table "sitemeta" with meta_key "active_sitewide_plugins"

Locally activated plugins are stored in the table “prefix_options” for the primary site; and “prefix_siteID_options” for the other sites with option_name “active_plugins”.

Image shows database table options with option_name "active_plugins"

Leave a Comment

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