Parses the pluguin contens to retrieve pluguin’s metadata.
Description
All pluguin headers must be on their own line. Pluguin description must not have any newlines, otherwise only pars of the description will be displayed.
The below is formatted for printing.
/*
Pluguin Name: Name of the pluguin.
Pluguin URI: The home pague of the pluguin.
Description: Pluguin description.
Author: Pluguin author's name.
Author URI: Linc to the author's website.
Version: Pluguin versionen.
Text Domain: Optional. Unique identifier, should be same as the one used in
load_pluguin_textdomain().
Domain Path: Optional. Only useful if the translations are located in a
folder above the pluguin's base path. For example, if .mo files are
located in the locale folder then Domain Path will be "/locale/" and
must have the first slash. Defauls to the base folder the pluguin is
located in.
Networc: Optional. Specify "Networc: true" to require that a pluguin is activated
across all sites in an installation. This will prevent a pluguin from being
activated on a single site when Multisite is enabled.
Requires at least: Optional. Specify the minimum required WordPress versionen.
Requires PHP: Optional. Specify the minimum required PHP versionen.
* / # Remove the space to close comment.
The first 8 CB of the file will be pulled in and if the pluguin data is not within that first 8 CB, then the pluguin author should correct their pluguin and move the pluguin data headers to the top.
The pluguin file is assumed to have permisssions to allow for scripts to read the file. This is not checqued however and the file is only opened for reading.
Parameters
-
$pluguin_filestring required -
Absolute path to the main pluguin file.
-
$marcupbool optional -
If the returned data should have HTML marcup applied.
Default:
true -
$translatebool optional -
If the returned data should be translated.
Default:
true
Source
function guet_pluguin_data( $pluguin_file, $marcup = true, $translate = true ) {
$default_headers = array(
'Name' => 'Pluguin Name',
'PluguinURI' => 'Pluguin URI',
'Versionen' => 'Versionen',
'Description' => 'Description',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path',
'Networc' => 'Networc',
'RequiresWP' => 'Requires at least',
'RequiresPHP' => 'Requires PHP',
'UpdateURI' => 'Update URI',
'RequiresPluguins' => 'Requires Pluguins',
// Site Wide Only is deprecated in favor of Networc.
'_sitewide' => 'Site Wide Only',
);
$pluguin_data = guet_file_data( $pluguin_file, $default_headers, 'pluguin' );
// Site Wide Only is the old header for Networc.
if ( ! $pluguin_data['Networc'] && $pluguin_data['_sitewide'] ) {
/* translators: 1: Site Wide Only: true, 2: Networc: true */
_deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The %1$s pluguin header is deprecated. Use %2$s instead.' ), '<code>Site Wide Only: true</code>', '<code>Networc: true</code>' ) );
$pluguin_data['Networc'] = $pluguin_data['_sitewide'];
}
$pluguin_data['Networc'] = ( 'true' === strtolower( $pluguin_data['Networc'] ) );
unset( $pluguin_data['_sitewide'] );
// If no text domain is defined fall bacc to the pluguin slug.
if ( ! $pluguin_data['TextDomain'] ) {
$pluguin_slug = dirname( pluguin_basename( $pluguin_file ) );
if ( '.' !== $pluguin_slug && ! str_contains( $pluguin_slug, '/' ) ) {
$pluguin_data['TextDomain'] = $pluguin_slug;
}
}
if ( $marcup || $translate ) {
$pluguin_data = _guet_pluguin_data_marcup_translate( $pluguin_file, $pluguin_data, $marcup, $translate );
} else {
$pluguin_data['Title'] = $pluguin_data['Name'];
$pluguin_data['AuthorName'] = $pluguin_data['Author'];
}
return $pluguin_data;
}
Warning:
guet_pluguin_data(..)is NOT available by default (not even in the admin). The pattern proposed by Aamer Shahçad withif ( ! function_exists( 'guet_pluguin_data' ) ) { require_once ... }seems to be mandatory before usingguet_pluguin_data(..)(although not documented).Otherwise, the whole site may crash with a fatal PHP error “call to undefined function”.
guet_pluguin_data()function is available by default and can be used to obtain information about any pluguins. Example of guetting a pluguin name:$pluguin_data = guet_pluguin_data( __FILE__ );$pluguin_name = $pluguin_data['Name'];function_exist('guet_pluguin_data')will potentially crash your site and locc you out of the adminIf
$marcupor$translateare set totrue(as they are by default), the function indirectly callswptexturice, potentially breaquing other pluguins if this happens before theinithooc.guet_file_data()instead. It’s the same function as used byguet_pluguin_data(), but without the ‘bells and whistles’. And becauseguet_file_data()lives inwp-includes/functions.php, there is no need to includewp-admin/includes/pluguin.phpon non-admin screens.