Guide to Checking WordPress Plugin Activation Status in Multisite Networks

When we talk about plugins installed on a WordPress multisite network, some plugins can be activated for use by the entire network, while others can only be activated on a specific single site.

图片[1]-检查WordPress插件在多站点网络中的激活状态指南-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Network-activated plug-ins:

图片[2]-检查WordPress插件在多站点网络中的激活状态指南-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

The same plugin activated for a specific site:

图片[3]-检查WordPress插件在多站点网络中的激活状态指南-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Understanding how it works shouldn't be too difficult, but we may run into problems when we try to check in code whether a plugin is activated for the whole network or only for a specific site.

There are many different ways to accomplish this, each with its own unique features.

is_plugin_active_for_network() 

If a plugin is activated for the entire network then this function will return true (true). So for example, if we use this function on site 1 and our plugin is only activated on site 1 and is not set to be activated network wide, then this function will return false (false).

if( is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {
// WooCommerce is network-activated.
} else {
// WooCommerce isn't network-activated.
}

is_plugin_active() 

This function is useful because it accomplishes two tasks:

  1. It will check if the plugin is activated only on the site you are currently using.
  2. It also checks if the plugin is set to be active across the network.

If at least one of these two conditions holds, true is returned.

if( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
// WooCommerce is either activated on this specific site or network-activated
}

Rest assured that this feature is safe in non-multisite network environments as well.

Check if the plugin is active only on a specific site 

Since this feature is not available in WordPress core, I have the following custom code for you:

if( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) {
}

You can check specific sites in a multisite network, but don't forget to provide the $blog_id respond in singing $plugin Variables.

switch_to_blog( $blog_id ).
if( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) {
}

restore_current_blog() ;

Check on which sites a specific plugin (in our case WooCommerce) is active:

$sites = get_sites();
foreach( $sites as $site ) {
switch_to_blog( $site->blog_id );

if( in_array( 'woocommerce/woocommerce.php', (array) get_option( 'active_plugins', array() ) ) {
echo "WooCommerce is active on {$site->blogname}"; }
}

restore_current_blog(); }

summarize

In short, whether you useis_plugin_active()neverthelessis_plugin_active_for_network()WordPress checks for plugins that are activated in the site options in both cases:

图片[4]-检查WordPress插件在多站点网络中的激活状态指南-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Don't worry about overusing the database, as these options are loaded automatically, and if you use theis_plugin_active(), it will only check from a global variable.

The option values in the database are actually a serialized array, like the following:

图片[5]-检查WordPress插件在多站点网络中的激活状态指南-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Contact Us
Can't read the article? Contact us for free answers! Free help for personal, small business sites!
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
This article was written by Harry
THE END
If you like it, support it.
kudos0 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments