get_plugin_data()WP 1.5.0

Gets plugin data (specified in the headers) from the given path to the main plugin file.

All data is taken from the plugin headers. Plugin data should be on a separate line. There should be no line breaks in the plugin descriptions; otherwise, only part of the description will be shown.

The header format is as follows:

/*
 * Plugin Name: Name
 * Plugin URI: Link to plugin info
 * Description: Description
 * Author: Author name
 * Author URI: Author link
 * Version: Plugin version
 * ...
 */

All plugin headers see here.

The function processes only the first 8kiB (6000-8000 characters) of information (see get_file_data()). If the plugin data happens to be larger, the author will have to reduce it or move the plugin data to the top (some authors add licensing information, etc., before the data).

The function is invoked (works) only in the admin panel, starting from the action admin_init. That is, during the actions: plugins_loaded, init, wp_loaded the function is not yet defined.

When you need to get data earlier, you can use a similar function get_file_data().

If $markup = true or $translate = true (as default), the function indirectly calls wptexturize(), potentially breaking other plugins if this occurs before the hook init.

Used By: get_plugins()

No Hooks.

Returns

Array. Array of data:

Array (
	[Name]        => Democracy Poll
	[PluginURI]   => http://wp-kama.ru/id_67/democracy-poll.html
	[Version]     => 4.5.4
	[Description] => Allows creating polls.
	[Author]      => <a href="https://wp-kama.ru/">Kama</a>
	[AuthorURI]   => http://wp-kama.ru/
	[AuthorName]  => Kama
	[TextDomain]  => dem
	[DomainPath]  => languages
	[Network]     =>
	[Title]       => <a href="https://wp-kama.ru/id_67/democracy-poll.html">Democracy Poll</a>
	[RequiresWP]  => 5.5
	[RequiresPHP] => 7.0
)

Mapping terms: the array value is the string that should be in the file, the key corresponds to the key of the returned array:

$default_headers = array(
	'Name'        => 'Plugin Name',
	'PluginURI'   => 'Plugin URI',
	'Version'     => 'Version',
	'Description' => 'Description',
	'Author'      => 'Author',
	'AuthorURI'   => 'Author URI',
	'TextDomain'  => 'Text Domain',
	'DomainPath'  => 'Domain Path',
	'Network'     => 'Network',
	'RequiresWP'  => 'Requires at least',
	'RequiresPHP' => 'Requires PHP',
	'UpdateURI'   => 'Update URI',
);

Usage

get_plugin_data( $plugin_file, $markup, $translate );
$plugin_file(string) (required)
Path to the main plugin file. E.g., __FILE__.
$markup(boolean)
Output HTML markup in the data (true) or not (false).
Default: true
$translate(boolean)
Translate the data or not.
Default: true

Examples

1

#1 Get the plugin data

An example of how you can get plugin data when you create a plugin.

Although, I can hardly imagine where this might be needed, so let it be a demonstration example:

add_action( 'admin_init', function(){

	// add function if needed
	if( ! function_exists('get_plugin_data') ){
		require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
	}

	$plugin_data = get_plugin_data( __FILE__ );

	// print the name of the plugin
	echo $plugin_data['Name']; 

	// print all the data

	print_r( $plugin_data );
} );

Another alternative (depending on circumstances) could be to use get_file_data() instead. It’s the same function as used by get_plugin_data(), but without the ‘bells and whistles’. And because get_file_data() lives in wp-includes/functions.php, there is no need to include wp-admin/includes/plugin.php on non-admin screens.

Changelog

Since 1.5.0 Introduced.
Since 5.3.0 Added support for Requires at least and Requires PHP headers.
Since 5.8.0 Added support for Update URI header.
Since 6.5.0 Added support for Requires Plugins header.

get_plugin_data() code WP 6.9

function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {

	$default_headers = array(
		'Name'            => 'Plugin Name',
		'PluginURI'       => 'Plugin URI',
		'Version'         => 'Version',
		'Description'     => 'Description',
		'Author'          => 'Author',
		'AuthorURI'       => 'Author URI',
		'TextDomain'      => 'Text Domain',
		'DomainPath'      => 'Domain Path',
		'Network'         => 'Network',
		'RequiresWP'      => 'Requires at least',
		'RequiresPHP'     => 'Requires PHP',
		'UpdateURI'       => 'Update URI',
		'RequiresPlugins' => 'Requires Plugins',
		// Site Wide Only is deprecated in favor of Network.
		'_sitewide'       => 'Site Wide Only',
	);

	$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );

	// Site Wide Only is the old header for Network.
	if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
		/* translators: 1: Site Wide Only: true, 2: Network: true */
		_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>' ) );
		$plugin_data['Network'] = $plugin_data['_sitewide'];
	}
	$plugin_data['Network'] = ( 'true' === strtolower( $plugin_data['Network'] ) );
	unset( $plugin_data['_sitewide'] );

	// If no text domain is defined fall back to the plugin slug.
	if ( ! $plugin_data['TextDomain'] ) {
		$plugin_slug = dirname( plugin_basename( $plugin_file ) );
		if ( '.' !== $plugin_slug && ! str_contains( $plugin_slug, '/' ) ) {
			$plugin_data['TextDomain'] = $plugin_slug;
		}
	}

	if ( $markup || $translate ) {
		$plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
	} else {
		$plugin_data['Title']      = $plugin_data['Name'];
		$plugin_data['AuthorName'] = $plugin_data['Author'];
	}

	return $plugin_data;
}