函数文档

wp_get_active_network_plugins()

💡 云策文档标注

概述

wp_get_active_network_plugins() 函数用于返回网络激活插件的文件路径数组,这些文件将被包含在全局作用域中。它从网络选项获取激活的插件列表,并验证文件的有效性。

关键要点

  • 函数返回一个字符串数组,包含网络激活插件的绝对路径。
  • 默认插件目录为 wp-content/plugins,可通过定义 WP_PLUGIN_DIR 和 WP_PLUGIN_URL 常量自定义。
  • 函数内部使用 get_site_option() 获取激活插件列表,并验证文件是否存在、以 .php 结尾且通过 validate_file() 检查。
  • 仅在多站点环境下使用此函数,建议先检查 is_multisite()。

注意事项

  • 此函数标记为私有,未来可能无法访问,可考虑使用 get_site_option('active_sitewide_plugins') 作为替代。

📄 原文内容

Returns array of network plugin files to be included in global scope.

Description

The default directory is wp-content/plugins. To change the default directory manually, define WP_PLUGIN_DIR and WP_PLUGIN_URL in wp-config.php.

Return

string[] Array of absolute paths to files to include.

Source

function wp_get_active_network_plugins() {
	$active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
	if ( empty( $active_plugins ) ) {
		return array();
	}

	$plugins        = array();
	$active_plugins = array_keys( $active_plugins );
	sort( $active_plugins );

	foreach ( $active_plugins as $plugin ) {
		if ( ! validate_file( $plugin )                     // $plugin must validate as file.
			&& str_ends_with( $plugin, '.php' )             // $plugin must end with '.php'.
			&& file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist.
			) {
			$plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
		}
	}

	return $plugins;
}

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes