返回介绍

get_file_data()

发布于 2017-09-10 23:21:00 字数 4089 浏览 1059 评论 0 收藏 0

get_file_data( string $file,  array $default_headers,  string $context = '' )

Retrieve metadata from a file.


description

Searches for metadata in the first 8kiB of a file, such as a plugin or theme. Each piece of metadata must be on its own line. Fields can not span multiple lines, the value will get cut at the end of the first line.

If the file data is not within that first 8kiB, then the author should correct their plugin file and move the data headers to the top.


参数

$file

(string) (Required) Path to the file.

$default_headers

(array) (Required) List of headers, in the format array('HeaderKey' => 'Header Name').

$context

(string) (Optional) If specified adds filter hook 'extra_$context_headers'.

Default value: ''


返回值

(array) Array of file headers in HeaderKey => Header Value format.


源代码

File: wp-includes/functions.php

function get_file_data( $file, $default_headers, $context = '' ) {
	// We don't need to write to the file, so just open for reading.
	$fp = fopen( $file, 'r' );

	// Pull only the first 8kiB of the file in.
	$file_data = fread( $fp, 8192 );

	// PHP will close file handle, but we are good citizens.
	fclose( $fp );

	// Make sure we catch CR-only line endings.
	$file_data = str_replace( "\r", "\n", $file_data );

	/**
	 * Filters extra file headers by context.
	 *
	 * The dynamic portion of the hook name, `$context`, refers to
	 * the context where extra headers might be loaded.
	 *
	 * @since 2.9.0
	 *
	 * @param array $extra_context_headers Empty array by default.
	 */
	if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) {
		$extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values
		$all_headers = array_merge( $extra_headers, (array) $default_headers );
	} else {
		$all_headers = $default_headers;
	}

	foreach ( $all_headers as $field => $regex ) {
		if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] )
			$all_headers[ $field ] = _cleanup_header_comment( $match[1] );
		else
			$all_headers[ $field ] = '';
	}

	return $all_headers;
}

更新日志

Versiondescription
2.9.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: _cleanup_header_comment()
  • wp-includes/functions.php: extra_{$context}_headers
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-admin/includes/class-theme-upgrader.php: Theme_Upgrader::check_package()
  • wp-admin/includes/plugin.php: get_plugin_data()
  • wp-includes/l10n.php: wp_get_pomo_file_data()
  • wp-includes/class-wp-theme.php: WP_Theme::__construct()

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 hozyali

    Reading multiple header keys

    <?php
    	$filedata = get_file_data(plugin_dir_path(__FILE__) . '/dirName/index.php', array(
        'Page Name' => 'Page Name',
        'Preview Image' => 'Preview Image',
        'Template File' => 'Template File',
            ));
    	print_r($filedata);
    	?>

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

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

发布评论

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