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

Retrieve metadata from a file.


Description Description

Searches for metadata in the first 8 KB 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 8 KB, then the author should correct their plugin file and move the data headers to the top.


Parameters Parameters

$file

(string) (Required) Absolute 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: ''


Top ↑

Return Return

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


Top ↑

Source Source

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 8 KB of the file in.
	$file_data = fread( $fp, 8 * KB_IN_BYTES );

	// 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.
	 */
	$extra_headers = $context ? apply_filters( "extra_{$context}_headers", array() ) : array();
	if ( $extra_headers ) {
		$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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.9.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 2 content
    Contributed by Josh Smith
    /**
     * Register stylesheet and scripts
     * Set the version of each the version of the plugin. 
     */
    function register_plugin_styles_scripts() {
    	// Set the file path to a variable.
    	$file_path = plugin_dir_path( __DIR__ ) . 'example-plugin-file.php';
    	// Read the version number from the main plugin file then set it to a variable.
    	$plugin_data = get_file_data( $file_path, array(
    		'Version' => 'Version'
    	) );
    
    	if ( ! empty( $plugin_data['Version'] ) ) {
    		// The the value of the Version header to a variable.
    		$ver = $plugin_data['Version'];
    
    		// Use the variable, $ver, in more than one stylesheet/script to bust cache when the plugin gets updated.
    		wp_enqueue_style( 'public-style', plugins_url( 'path/to/stylesheet.css' ), array(), $ver );
    		wp_enqueue_script('public-script', plugins_url( 'path/to/script.js' ), array(), $ver , true);
    	}
    }

You must log in before being able to contribute a note or feedback.