get_file_data() - WordPress 函数参考中文文档

返回介绍

get_file_data()

发布于 2017-09-10 字数4091 浏览 899 评论 0

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

Retrieve metadata from a file.


描述

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;
}

更新日志

Version 描述
2.9.0 Introduced.

相关函数

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);
    	?>

上一篇:get_filesystem_method()

下一篇:get_file_描述()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!