get_plugins

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

Description

(array) get_plugins( (string) $plugin_folder = '' ); 

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.

Returns (array)

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

Parameters (1)

0. $plugin_folder — Optional. (string) => ''
Relative path to single plugin folder.

Usage

  1. if ( !function_exists( 'get_plugins' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/plugin.php'; 
  3.  
  4. // Optional. Relative path to single plugin folder. 
  5. $plugin_folder = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_plugins($plugin_folder); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/plugin.php  
  1. function get_plugins($plugin_folder = '') { 
  2.  
  3. if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') ) 
  4. $cache_plugins = array(); 
  5.  
  6. if ( isset($cache_plugins[ $plugin_folder ]) ) 
  7. return $cache_plugins[ $plugin_folder ]; 
  8.  
  9. $wp_plugins = array (); 
  10. $plugin_root = WP_PLUGIN_DIR
  11. if ( !empty($plugin_folder) ) 
  12. $plugin_root .= $plugin_folder; 
  13.  
  14. // Files in wp-content/plugins directory 
  15. $plugins_dir = @ opendir( $plugin_root); 
  16. $plugin_files = array(); 
  17. if ( $plugins_dir ) { 
  18. while (($file = readdir( $plugins_dir ) ) !== false ) { 
  19. if ( substr($file, 0, 1) == '.' ) 
  20. continue; 
  21. if ( is_dir( $plugin_root.'/'.$file ) ) { 
  22. $plugins_subdir = @ opendir( $plugin_root.'/'.$file ); 
  23. if ( $plugins_subdir ) { 
  24. while (($subfile = readdir( $plugins_subdir ) ) !== false ) { 
  25. if ( substr($subfile, 0, 1) == '.' ) 
  26. continue; 
  27. if ( substr($subfile, -4) == '.php' ) 
  28. $plugin_files[] = "$file/$subfile"; 
  29. closedir( $plugins_subdir ); 
  30. } else { 
  31. if ( substr($file, -4) == '.php' ) 
  32. $plugin_files[] = $file; 
  33. closedir( $plugins_dir ); 
  34.  
  35. if ( empty($plugin_files) ) 
  36. return $wp_plugins; 
  37.  
  38. foreach ( $plugin_files as $plugin_file ) { 
  39. if ( !is_readable( "$plugin_root/$plugin_file" ) ) 
  40. continue; 
  41.  
  42. $plugin_data = get_plugin_data( "$plugin_root/$plugin_file", false, false ); //Do not apply markup/translate as it'll be cached. 
  43.  
  44. if ( empty ( $plugin_data['Name'] ) ) 
  45. continue; 
  46.  
  47. $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data; 
  48.  
  49. uasort( $wp_plugins, '_sort_uname_callback' ); 
  50.  
  51. $cache_plugins[ $plugin_folder ] = $wp_plugins; 
  52. wp_cache_set('plugins', $cache_plugins, 'plugins'); 
  53.  
  54. return $wp_plugins;