wppaste
WordPress

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

Since
2.9.0
Source
wp-includes/functions.php:6891
Retrieves metadata from a file.

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.

Compatibility

WordPress
since 2.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$filestring
Absolute path to the file.
$default_headersarray
List of headers, in the format array( 'HeaderKey' => 'Header Name' ).
$contextstringoptional
If specified adds filter hook 'extra_$context_headers'.
Default empty string.Default: ''

Return value

string[]
Array of file header values keyed by header name.

Performance profile

How much work a call to get_file_data() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via file_get_contents().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
26–47

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 75.

Plugin surface
1 hook

Third-party callbacks on 'extra_{$context}_headers' run inside this call, and their cost is not bounded by anything here.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_get_contents()called directly

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_file_data() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always26–28file_get_contents()
always34–36file_get_contents(), apply_filters()
always37–39file_get_contents(), array_combine(), array_merge()
always45–47file_get_contents(), apply_filters(), array_combine(), array_merge()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7426–4671 fewer instruction than PHP 8.5
8.57526–4771 more instruction than PHP 8.4
8.47426–4673 fewer instructions than PHP 8.3
8.37728–497
8.27728–497
8.17728–497
7.47728–497

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while get_file_data() runs, in this order:

  1. apply_filters( extra_{$context}_headers )filterline 6912 (+21 into the body)

    Filters extra file headers by context.

Uses · 2

Used by · 5

Source code

function get_file_data( $file, $default_headers, $context = '' ) {	// Pull only the first 8 KB of the file in.	$file_data = file_get_contents( $file, false, null, 0, 8 * KB_IN_BYTES ); 	if ( false === $file_data ) {		$file_data = '';	} 	// 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]*<\?php)?[ \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;}

Changelog

Introduced in 2.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.