检查plugins目录并使用插件数据检索所有插件文件。

原型

get_plugins( string $plugin_folder = '' )

描述

WordPress仅支持基本插件目录(wp-content / plugins)和插件目录(wp-content / plugins / my-plugin)上方的一个目录中的插件文件。它查找的文件包含插件数据,必须在这两个位置找到。建议将插件文件保存在自己的目录中。

参数

$plugin_folder (string) (Optional)  单个插件文件夹的相对路径。

返回值

(array)  Key是插件文件路径,值是插件数据的数组。

源文件

路径:wp-admin/includes/plugin.php

<?php
...
function get_plugins($plugin_folder = '') {
	if ( ! $cache_plugins = wp_cache_get('plugins', '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 ( substr($file, 0, 1) == '.' )
				continue;
			if ( is_dir( $plugin_root.'/'.$file ) ) {
				$plugins_subdir = @ opendir( $plugin_root.'/'.$file );
				if ( $plugins_subdir ) {
					while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
						if ( substr($subfile, 0, 1) == '.' )
							continue;
						if ( substr($subfile, -4) == '.php' )
							$plugin_files[] = "$file/$subfile";
					}
					closedir( $plugins_subdir );
				}
			} else {
				if ( substr($file, -4) == '.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;
		$plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached.
		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;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_plugins/