返回介绍

get_plugin_data()

发布于 2017-09-10 23:46:59 字数 7058 浏览 1015 评论 0 收藏 0

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

Parses the plugin contents to retrieve plugin’s metadata.


description

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.

/*
Plugin Name: Name of Plugin
Plugin URI: Link to plugin information
description: Plugin description
Author: Plugin author's name
Author URI: Link to the author's web site
Version: Must be set in the plugin for WordPress 2.3+
Text Domain: Optional. Unique identifier, should be same as the one used in
    load_plugin_textdomain()
Domain Path: Optional. Only useful if the translations are located in a
    folder above the plugin'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. Defaults to the base folder the plugin is
    located in.
Network: Optional. Specify "Network: true" to require that a plugin is activated
    across all sites in an installation. This will prevent a plugin from being
    activated on a single site when Multisite is enabled.
 * /

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.


参数

$plugin_file

(string) (Required) Path to the main plugin file.

$markup

(bool) (Optional) If the returned data should have HTML markup applied.

Default value: true

$translate

(bool) (Optional) If the returned data should be translated.

Default value: true


返回值

(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. }


源代码

File: wp-admin/includes/plugin.php

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',
		// 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 && false === strpos( $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;
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

Uses

  • wp-admin/includes/plugin.php: _get_plugin_data_markup_translate()
  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: get_file_data()
  • wp-includes/functions.php: _deprecated_argument()
  • wp-includes/plugin.php: plugin_basename()

Used By

  • wp-admin/includes/ajax-actions.php: wp_ajax_delete_plugin()
  • wp-admin/includes/ajax-actions.php: wp_ajax_update_plugin()
  • wp-admin/includes/class-wp-automatic-updater.php: WP_Automatic_Updater::update()
  • wp-admin/includes/class-plugin-upgrader.php: Plugin_Upgrader::bulk_upgrade()
  • wp-admin/includes/class-plugin-upgrader.php: Plugin_Upgrader::check_package()
  • wp-admin/includes/plugin.php: get_dropins()
  • wp-admin/includes/plugin.php: is_network_only_plugin()
  • wp-admin/includes/plugin.php: get_plugins()
  • wp-admin/includes/plugin.php: get_mu_plugins()
  • Show 4 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by pepe

    If $markup or $translate are set to true (as they are by default), the function indirectly calls wptexturize, potentially breaking other plugins if this happens before the init hook.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文