WordPress at a glance

get_file_data() WP 2.9.0

Retrieve metadata from a file (it's specified in the header comment of the 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.

Is the basis for: get_plugin_data()
✈ 1 time = 0.000433s = fast | 50000 times = 0.66s = very fast | PHP 7.0.8, WP 4.6.1
Hooks from the function
Return

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

{{{я второй аргумент не не стал менять описание ибо тут даже понятнее описание. но русский тоже не стал трогать}}}

Usage

get_file_data( $file, $default_headers, $context );
$file(string) (required)
Absolute path to the file.
$default_headers(array) (required)
List of headers, in the format array('HeaderKey' => 'Header Name').
$context(string)
If specified adds filter hook {@see 'extra_$context_headers'}.
Default: ''

Examples

#1 Get plugin's data

Suppose we have created a plugin and its main file has such header:

/*
Plugin Name: Name
Description: Description
Author: Kama
Version: 1.0
*/
// Path to the file of the plugin. In the file of the plugin it can be retrived using magic constant __FILE__
$file = '/home/site.ru/wp-content/plugins/myplugin/index.php';
$data = get_file_data( $file, array('ver'=>'Version', 'author'=>'Author', 'name'=>'Plugin Name' ) );

/* $data will contain:
Array
(
	[ver] => 1.0
	[author] => Kama
	[name] => Name
)
*/

#2 Dynamic version of the plugin

Suppose we're writing a plugin and it has PLUGIN_VERSION constant which represents the version of the plugin. To not change it every time we update the plugin, we can parse the plugin's metadata for the version using this function:

/*
Plugin Name: Plugin Name
Plugin URI: http://wp-kama.com
Description:  Plugin Description
Author: Kama
Version: 1.0
*/
$data = get_file_data( __FILE__, array('ver'=>'Version') );
define('PLUGIN_VERSION', $data['ver'] );

Changelog

Since 2.9.0 Introduced.

Code of get file data: wp-includes/functions.php WP 5.2.2

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

Related Functions

From tag: Files data (get file info)

More from category: Plugins, Hooks

No comments
    Hello, !     Log In . Register