wppaste
WordPress

get_plugins( string $plugin_folder = '' ): array[]

Since
1.5.0
Source
wp-admin/includes/plugin.php:279
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.

Compatibility

WordPress
since 1.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$plugin_folderstringoptional
Relative path to single plugin folder.Default: ''

Return value

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

Performance profile

How much work a call to get_plugins() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Only touches the object cache, via wp_cache_get().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
11–50

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 124.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
24

24 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_get()called directly

Further down the call graph this can also reach hook, query, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_plugins() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
isset($cache_plugins[$plugin_folder])11–12wp_cache_get()
!isset($cache_plugins[$plugin_folder]) && empty($plugin_files)24–26wp_cache_get(), opendir()
!isset($cache_plugins[$plugin_folder]) && $file === false && empty($plugin_files)34–36wp_cache_get(), opendir(), readdir(), closedir()
!isset($cache_plugins[$plugin_folder]) && !empty($plugin_files)37–40wp_cache_get(), opendir(), uasort(), wp_cache_set()
!isset($cache_plugins[$plugin_folder]) && $file === false && !empty($plugin_files)47–50wp_cache_get(), opendir(), readdir(), closedir(), uasort(), wp_cache_set()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12411–5017
8.512411–5017
8.412411–50176 fewer instructions than PHP 8.3
8.313011–5017
8.213011–50172 more instructions than PHP 8.1
8.112811–9417
7.412811–9417

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

Used by · 24

Show all 24

Source code

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

Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/plugin.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.