get_plugins( string $plugin_folder = '' ): array[]
- Since
- 1.5.0
- Source
wp-admin/includes/plugin.php:279
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
- Scaling
- Scales with input
- Instructions
- 11–50
- Plugin surface
- None
- Called by
- 24
Only touches the object cache, via wp_cache_get().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 124.
Nothing here hands control to plugin code.
24 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
isset($cache_plugins[$plugin_folder]) | 11–12 | wp_cache_get() |
!isset($cache_plugins[$plugin_folder]) && empty($plugin_files) | 24–26 | wp_cache_get(), opendir() |
!isset($cache_plugins[$plugin_folder]) && $file === false && empty($plugin_files) | 34–36 | wp_cache_get(), opendir(), readdir(), closedir() |
!isset($cache_plugins[$plugin_folder]) && !empty($plugin_files) | 37–40 | wp_cache_get(), opendir(), uasort(), wp_cache_set() |
!isset($cache_plugins[$plugin_folder]) && $file === false && !empty($plugin_files) | 47–50 | wp_cache_get(), opendir(), readdir(), closedir(), uasort(), wp_cache_set() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 124 | 11–50 | 17 | |
| 8.5 | 124 | 11–50 | 17 | |
| 8.4 | 124 | 11–50 | 17 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 130 | 11–50 | 17 | |
| 8.2 | 130 | 11–50 | 17 | 2 more instructions than PHP 8.1 |
| 8.1 | 128 | 11–94 | 17 | |
| 7.4 | 128 | 11–94 | 17 |
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
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- get_plugin_data()Parses the plugin contents to retrieve plugin's metadata.
- plugin_basename()Gets the basename of a plugin.
- wp_cache_set()Saves the data to the cache.
Used by · 24
- Language_Pack_Upgrader::get_name_for_update()Gets the name of an item being updated.
- Plugin_Installer_Skin::do_overwrite()Checks if the plugin can be overwritten and outputs the HTML for overwriting a plugin on upload.
- Plugin_Upgrader::plugin_info()Retrieves the path to the file that contains the plugin info.
- WP_Debug_Data::get_wp_plugins_raw_data()Gets the raw plugin data for the WordPress active and inactive sections of the debug data.
- WP_Plugin_Dependencies::get_plugins()Gets data for installed plugins.
- WP_Plugins_List_Table::prepare_items()
- WP_REST_Block_Directory_Controller::find_plugin_for_slug()Finds an installed plugin for the given slug.
- WP_REST_Plugins_Controller::get_items()Retrieves a collection of plugins.
- WP_REST_Plugins_Controller::get_plugin_data()Gets the plugin header data for a plugin.
- WP_REST_Templates_Controller::get_wp_templates_author_text_field()Returns a human readable text for the author of the template.
- WP_Recovery_Mode_Email_Service::get_plugin()Return the details for a single plugin based on the extension data from an error.
- WP_Site_Health::get_test_plugin_version()Tests if plugins are outdated, or unnecessary.
Show all 24
- _get_plugin_from_callback()Internal helper function to find the plugin from a meta box callback.
- do_block_editor_incompatible_meta_box()Renders a "fake" meta box with an information message, shown on the block editor, when an incompatible meta box is found.
- get_plugin_updates()Retrieves plugins with updates available.
- install_plugin_install_status()Determines the status we can perform on a plugin.
- validate_plugin()Validates the plugin path.
- wp_ajax_toggle_auto_updates()Handles enabling or disable plugin and theme auto-updates via AJAX.
- wp_ajax_update_plugin()Handles updating a plugin via AJAX.
- wp_dashboard_plugins_output()Display plugins text for the WordPress news widget.
- wp_edit_theme_plugin_file()Attempts to edit a file for a theme or plugin.
- wp_get_plugin_action_button()Gets the markup for the plugin install action button.
- wp_link_manager_disabled_message()Outputs the 'disabled' message for the WordPress Link Manager.
- wp_update_plugins()Checks for available updates to plugins based on the latest versions hosted on WordPress.org.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.