函数文档

is_plugin_active()

💡 云策文档标注

概述

is_plugin_active() 函数用于检查指定插件是否处于激活状态。它仅适用于 plugins/ 目录下的插件,对于 mu-plugins/ 目录下的插件会返回 false。

关键要点

  • 函数接受一个必需参数:插件文件相对于 plugins 目录的路径字符串。
  • 返回布尔值:true 表示插件在激活列表中,false 表示不在。
  • 函数内部通过检查 active_plugins 选项或使用 is_plugin_active_for_network() 来判断插件状态。
  • 注意:此函数不适用于 must-use 插件,且建议直接使用函数而非复制其定义,以保持兼容性。

代码示例

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

注意事项

  • 函数可在前端和后端使用,但需确保正确包含相关文件(如使用 include_once)。
  • 相关函数包括 is_plugin_active_for_network() 和 get_option(),用于扩展功能。

📄 原文内容

Determines whether a plugin is active.

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 in the active plugins list. False, not in the list.

Source

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

Changelog

Version Description
2.5.0 Introduced.

User Contributed Notes