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修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/03/image-119-1024x576.png)
Network-activated plug-ins:
![图片[2]-检查WordPress插件在多站点网络中的激活状态指南-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/03/image-117.png)
The same plugin activated for a specific site:
![图片[3]-检查WordPress插件在多站点网络中的激活状态指南-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/03/image-118.png)
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:
- It will check if the plugin is activated only on the site you are currently using.
- 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修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/03/image-120-1024x281.png)
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修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/03/image-121.png)
Link to this article:https://www.361sale.com/en/6236The article is copyrighted and must be reproduced with attribution.
No comments