^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_file_data()用法示例

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.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

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()

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

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈