函数文档

wp_get_mu_plugins()

💡 云策文档标注

概述

wp_get_mu_plugins() 函数用于检索必须使用(must-use)插件文件的数组,默认目录为 wp-content/mu-plugins。开发者可通过定义 WPMU_PLUGIN_DIR 和 WPMU_PLUGIN_URL 来手动更改默认目录。

关键要点

  • 函数返回一个包含绝对路径的字符串数组,对应 mu-plugins 目录中的 .php 文件。
  • 默认目录为 wp-content/mu-plugins,但可通过在 wp-config.php 中定义 WPMU_PLUGIN_DIR 和 WPMU_PLUGIN_URL 来自定义。
  • 函数内部会检查目录是否存在和可读,并自动排序返回的数组。
  • 自 WordPress 3.0.0 版本引入此函数。

代码示例

function wp_get_mu_plugins() {
	$mu_plugins = array();

	if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
		return $mu_plugins;
	}

	$dh = opendir( WPMU_PLUGIN_DIR );
	if ( ! $dh ) {
		return $mu_plugins;
	}

	while ( ( $plugin = readdir( $dh ) ) !== false ) {
		if ( str_ends_with( $plugin, '.php' ) ) {
			$mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin;
		}
	}

	closedir( $dh );

	sort( $mu_plugins );

	return $mu_plugins;
}

📄 原文内容

Retrieves an array of must-use plugin files.

Description

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

Return

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

Source

function wp_get_mu_plugins() {
	$mu_plugins = array();

	if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
		return $mu_plugins;
	}

	$dh = opendir( WPMU_PLUGIN_DIR );
	if ( ! $dh ) {
		return $mu_plugins;
	}

	while ( ( $plugin = readdir( $dh ) ) !== false ) {
		if ( str_ends_with( $plugin, '.php' ) ) {
			$mu_plugins[] = WPMU_PLUGIN_DIR . '/' . $plugin;
		}
	}

	closedir( $dh );

	sort( $mu_plugins );

	return $mu_plugins;
}

Changelog

Version Description
3.0.0 Introduced.