is_plugin_active_for_network()
云策文档标注
概述
is_plugin_active_for_network() 函数用于判断插件是否在整个 WordPress 多站点网络中激活。它仅适用于 plugins/ 目录下的插件,不适用于 mu-plugins/ 目录下的插件。
关键要点
- 函数检查插件是否在网络范围内激活,返回布尔值 true 或 false。
- 仅适用于多站点环境,非多站点时直接返回 false。
- 插件文件路径需相对于 plugins 目录指定。
- 函数定义在 wp-admin/includes/plugin.php 中,在非管理页面使用时需先包含该文件。
代码示例
// 确保插件函数已定义
if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}
if ( is_plugin_active_for_network( 'plugin-directory/plugin-file.php' ) ) {
// 插件已激活
}注意事项
mu-plugins/ 文件夹中的插件无法被“激活”,因此此函数对它们始终返回 false。更多信息可参考主题开发者手册中的条件标签文章。
原文内容
Determines whether the plugin is active for the entire network.
Description
Only plugins installed in the plugins/ folder can be active.
Plugins in the mu-plugins/ folder can’t be “activated,” so this function will return false for those plugins.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Parameters
$pluginstringrequired-
Path to the plugin file relative to the plugins directory.
Source
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;
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 2 content
Codex
Example
// Makes sure the plugin is defined before trying to use it if ( ! function_exists( 'is_plugin_active_for_network' ) ) { require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); } if ( is_plugin_active_for_network( 'plugin-directory/plugin-file.php' ) ) { // Plugin is activated }