WordPress at a glance

get_plugins() WP 1.5.0

Gets all plugins data (active and inactive): file, name, author, etc. Works with the plugins directory.

The function will collect data from all main plugin files. These are files that have PHP comments describing the plugin (plugin headers). The data will contain a relative path to the plugin file and all the data from the comment of the file itself.

WordPress works with plugins located only in directories: wp-content/plugins and wp-content/mu-plugins. This function searches plugins files in these two directories only, so it is recommended to all plugin files be located in these directories.

There's a similiar function wp_get_themes()

The function caches the result when the parameter $plugin_folder is specified.

To use this function on front-end or in early state, you should include this file:

require_once ABSPATH . 'wp-admin/includes/plugin.php';
Works based on: get_plugin_data()
✈ 1 time = 0.008743s = very slow | 50000 times = 2.13s = fast | PHP 7.1.2, WP 4.7.4

No Hooks.

Return

Array. Key is the plugin file path and the value is an array of the plugin data.

Usage

get_plugins( $plugin_folder );
$plugin_folder(string)
Relative path to single plugin folder.
Default: ''

Examples

#1 Get the data of all plugins

The following code will return the data of all installed (maybe not active) plugins.

// Check if the get_plugins() if registered. If you're out of the dashboard
// Usually get_plugins() works only on the dashboard
if ( ! function_exists( 'get_plugins' ) ) {
	// include the file with get_plugins()
	require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

// get the plugins data
$all_plugins = get_plugins();

// Save the data to the error log, where you can see the resulting array
error_log( print_r( $all_plugins, true ) );

And the log contains:

Array(
	[hello-dolly/hello.php] => Array
		(
			[Name] => Hello Dolly
			[PluginURI] => http://wordpress.org/extend/plugins/hello-dolly/
			[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] => http://ma.tt/
			[TextDomain] => 
			[DomainPath] => 
			[Network] => 
			[Title] => Hello Dolly
			[AuthorName] => Matt Mullenweg

)
...
...

Changelog

Since 1.5.0 Introduced.

Code of get plugins: wp-admin/includes/plugin.php WP 5.2.2

<?php
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 ( 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;
}

Related Functions

From tag: plugin

More from category: Plugins, Hooks

No comments
    Hello, !     Log In . Register