You might wonder is there another way to solve this? At least that's what I did, because the solution @mfields posted didn't work for me.
After core code digging I found an pretty useful core helper function which will help us achieve about the same result. I'm talking about the function get_file_data()
.
As first argument we need a string of the absolute path to your main plugin file. The second argument is an array of the headers you want to retrieve, check the array below for all headers.
$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',
// Site Wide Only is deprecated in favor of Network.
'_sitewide' => 'Site Wide Only',
);
The third argument is going to be "plugin", don't tell me why without third argument it works as well.
Anyway this is what I use now (it's located in the main plugin file).
$plugin_data = get_file_data(__FILE__, [
'Version' => 'Version',
'TextDomain' => 'Text Domain'
], 'plugin');