函数文档

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.

Return

bool True if active for the network, otherwise false.

More Information

The file that defines this function (<a class="external text" href="https://core.trac.wordpress.org/browser/tags/5.3/src/wp-admin/includes/plugin.php#L0" rel="nofollow">wp-admin/includes/plugin.php</a>) is only loaded in the admin sections. In order to use is_plugin_active_for_network outside the admin pages, it’s necessary to include or require plugin.php before trying to use it (as shown in the example).

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.

User Contributed Notes