函数文档

get_plugins()

💡 云策文档标注

概述

get_plugins() 函数用于扫描 WordPress 插件目录,检索所有插件文件及其元数据。它支持插件根目录和子目录结构,并利用缓存机制提升性能。

关键要点

  • 扫描 wp-content/plugins 目录及其一级子目录,查找包含插件数据的 .php 文件。
  • 返回以插件文件名(如 hello-dolly/hello.php)为键的数组,每个元素包含 get_plugin_data() 解析的元数据。
  • 首次调用时结果会被缓存,建议在 'after_setup_theme' 动作后调用,以便插件和主题能过滤结果。
  • 可选参数 $plugin_folder 可指定相对路径,以限制扫描特定插件文件夹。
  • 如果遇到未定义函数错误,需包含 wp-admin/includes/plugin.php 文件。

代码示例

// 获取所有插件数据
$plugins = get_plugins();
print_r($plugins);

// 示例输出:数组包含插件文件名和元数据,如 [hello-dolly/hello.php] => Array(...)

注意事项

  • 插件文件应放置在 wp-content/plugins 或其子目录中,主执行文件需包含插件头信息。
  • 为优化维护性,建议将代码拆分,而非全部放在一个文件中。
  • 函数内部使用 wp_cache_get() 和 wp_cache_set() 进行缓存管理,避免重复扫描。

📄 原文内容

Checks the plugins directory and retrieve all plugin files with plugin data.

Description

WordPress only supports plugin files in the base plugins directory (wp-content/plugins) and in one directory above the plugins directory (wp-content/plugins/my-plugin). The file it looks for has the plugin data and must be found in those two locations. It is recommended to keep your plugin files in their own directories.

The file with the plugin data is the file that will be included and therefore needs to have the main execution for the plugin. This does not mean everything must be contained in the file and it is recommended that the file be split for maintainability. Keep everything in one file for extreme optimization purposes.

Parameters

$plugin_folderstringoptional
Relative path to single plugin folder.

Return

array[] Array of arrays of plugin data, keyed by plugin file name. See get_plugin_data() .

More Information

If you have `PHP Fatal error: Call to undefined function get_plugins()` then you must include the file ‘wp-admin/includes/plugin.php‘ like in example.

Results are cached on the first run of the function, therefore it is recommended to call the function at least after the ‘after_setup_theme‘ action so that plugins and themes have the ability to filter the results.

Source

function get_plugins( $plugin_folder = '' ) {

	$cache_plugins = wp_cache_get( 'plugins', 'plugins' );
	if ( ! $cache_plugins ) {
		$cache_plugins = array();
	}

	if ( isset( $cache_plugins[ $plugin_folder ] ) ) {
		return $cache_plugins[ $plugin_folder ];
	}

	$wp_plugins  = array();
	$plugin_root = WP_PLUGIN_DIR;
	if ( ! empty( $plugin_folder ) ) {
		$plugin_root .= $plugin_folder;
	}

	// Files in wp-content/plugins directory.
	$plugins_dir  = @opendir( $plugin_root );
	$plugin_files = array();

	if ( $plugins_dir ) {
		while ( ( $file = readdir( $plugins_dir ) ) !== false ) {
			if ( str_starts_with( $file, '.' ) ) {
				continue;
			}

			if ( is_dir( $plugin_root . '/' . $file ) ) {
				$plugins_subdir = @opendir( $plugin_root . '/' . $file );

				if ( $plugins_subdir ) {
					while ( ( $subfile = readdir( $plugins_subdir ) ) !== false ) {
						if ( str_starts_with( $subfile, '.' ) ) {
							continue;
						}

						if ( str_ends_with( $subfile, '.php' ) ) {
							$plugin_files[] = "$file/$subfile";
						}
					}

					closedir( $plugins_subdir );
				}
			} elseif ( str_ends_with( $file, '.php' ) ) {
				$plugin_files[] = $file;
			}
		}

		closedir( $plugins_dir );
	}

	if ( empty( $plugin_files ) ) {
		return $wp_plugins;
	}

	foreach ( $plugin_files as $plugin_file ) {
		if ( ! is_readable( "$plugin_root/$plugin_file" ) ) {
			continue;
		}

		// Do not apply markup/translate as it will be cached.
		$plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false );

		if ( empty( $plugin_data['Name'] ) ) {
			continue;
		}

		$wp_plugins[ plugin_basename( $plugin_file ) ] = $plugin_data;
	}

	uasort( $wp_plugins, '_sort_uname_callback' );

	$cache_plugins[ $plugin_folder ] = $wp_plugins;
	wp_cache_set( 'plugins', $cache_plugins, 'plugins' );

	return $wp_plugins;
}

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Get All Plugins

    The following code snippet returns all plugins installed on your site (not just activated ones).

    </pre>
    <p>Example output:</p>
    <pre class="wp-block-code"><code lang="php" class="language-php line-numbers">Array
    (
        [hello-dolly/hello.php] => Array
            (
                [Name] => Hello Dolly
                [PluginURI] => <a href="https://wordpress.org/extend/plugins/hello-dolly/" rel="nofollow ugc">https://wordpress.org/extend/plugins/hello-dolly/</a>
                [Version] => 1.6
                [Description] => This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
                [Author] => Matt Mullenweg
                [AuthorURI] => <a href="http://ma.tt/" rel="nofollow ugc">http://ma.tt/</a>
                [TextDomain] => 
                [DomainPath] => 
                [Network] => 
                [Title] => Hello Dolly
                [AuthorName] => Matt Mullenweg
    
    )