get_plugin_data

Parses the plugin contents to retrieve plugin's metadata.

Description

(array) get_plugin_data( (string) $plugin_file, (constant) $markup = true, (bool) $translate = true ); 

The metadata of the plugin's data searches for the following in the plugin's header. All plugin data must be on its own line. For plugin description, it must not have any newlines or only parts of the description will be displayed and the same goes for the plugin data. The below is formatted for printing.

Some users have issues with opening large files and manipulating the contents for want is usually the first 1kiB or 2kiB. This function stops pulling in the plugin contents when it has all of the required plugin data.

The first 8kiB of the file will be pulled in and if the plugin data is not within that first 8kiB, then the plugin author should correct their plugin and move the plugin data headers to the top.

The plugin file is assumed to have permissions to allow for scripts to read the file. This is not checked however and the file is only opened for reading.

Returns (array)

{ Plugin data. Values will be empty if not supplied by the plugin. @type string $Name Name of the plugin. Should be unique. @type string $Title Title of the plugin and link to the plugin's site (if set). @type string $Description Plugin description. @type string $Author Author's name. @type string $AuthorURI Author's website address (if set). @type string $Version Plugin version. @type string $TextDomain Plugin textdomain. @type string $DomainPath Plugins relative directory path to .mo files. @type bool $Network Whether the plugin can only be activated network-wide. }

Parameters (3)

0. $plugin_file (string)
Path to the plugin file
1. $markup — Optional. (constant) => true
If the returned data should have HTML markup applied. Default true.
2. $translate — Optional. (bool) => true
If the returned data should be translated. Default true.

Usage

  1. if ( !function_exists( 'get_plugin_data' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/plugin.php'; 
  3.  
  4. // Path to the plugin file 
  5. $plugin_file = ''; 
  6.  
  7. // Optional. If the returned data should have HTML markup applied. 
  8. // Default true. 
  9. $markup = true; 
  10.  
  11. // Optional. If the returned data should be translated. Default true. 
  12. $translate = true; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = get_plugin_data($plugin_file, $markup, $translate); 
  16.  

Defined (1)

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

/wp-admin/includes/plugin.php  
  1. function get_plugin_data( $plugin_file, $markup = true, $translate = true ) { 
  2.  
  3. $default_headers = array( 
  4. 'Name' => 'Plugin Name',  
  5. 'PluginURI' => 'Plugin URI',  
  6. 'Version' => 'Version',  
  7. 'Description' => 'Description',  
  8. 'Author' => 'Author',  
  9. 'AuthorURI' => 'Author URI',  
  10. 'TextDomain' => 'Text Domain',  
  11. 'DomainPath' => 'Domain Path',  
  12. 'Network' => 'Network',  
  13. // Site Wide Only is deprecated in favor of Network. 
  14. '_sitewide' => 'Site Wide Only',  
  15. ); 
  16.  
  17. $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' ); 
  18.  
  19. // Site Wide Only is the old header for Network 
  20. if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) { 
  21. /** translators: 1: Site Wide Only: true, 2: Network: true */ 
  22. _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The %1$s plugin header is deprecated. Use %2$s instead.' ), '<code>Site Wide Only: true</code>', '<code>Network: true</code>' ) ); 
  23. $plugin_data['Network'] = $plugin_data['_sitewide']; 
  24. $plugin_data['Network'] = ( 'true' == strtolower( $plugin_data['Network'] ) ); 
  25. unset( $plugin_data['_sitewide'] ); 
  26.  
  27. // If no text domain is defined fall back to the plugin slug. 
  28. if ( ! $plugin_data['TextDomain'] ) { 
  29. $plugin_slug = dirname( plugin_basename( $plugin_file ) ); 
  30. if ( '.' !== $plugin_slug && false === strpos( $plugin_slug, '/' ) ) { 
  31. $plugin_data['TextDomain'] = $plugin_slug; 
  32.  
  33. if ( $markup || $translate ) { 
  34. $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate ); 
  35. } else { 
  36. $plugin_data['Title'] = $plugin_data['Name']; 
  37. $plugin_data['AuthorName'] = $plugin_data['Author']; 
  38.  
  39. return $plugin_data;