get_file_data( string $file, array $default_headers, string $context = '' ): string[]
- Since
- 2.9.0
- Source
wp-includes/functions.php:6946
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
- Scaling
- Scales with input
- Instructions
- 26–47
- Plugin surface
- 1 hook
- Called by
- 5
Reads or writes the filesystem via file_get_contents().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 75.
Third-party callbacks on 'extra_{$context}_headers' run inside this call, and their cost is not bounded by anything here.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
file_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 26–28 | file_get_contents() |
| always | 34–36 | file_get_contents(), apply_filters() |
| always | 37–39 | file_get_contents(), array_combine(), array_merge() |
| always | 45–47 | file_get_contents(), apply_filters(), array_combine(), array_merge() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 74 | 26–46 | 7 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 75 | 26–47 | 7 | 1 more instruction than PHP 8.4 |
| 8.4 | 74 | 26–46 | 7 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 77 | 28–49 | 7 | |
| 8.2 | 77 | 28–49 | 7 | |
| 8.1 | 77 | 28–49 | 7 | |
| 7.4 | 77 | 28–49 | 7 |
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:
- apply_filters( extra_{$context}_headers )filterline 6967 (+21 into the body)
Filters extra file headers by context.
Uses · 2
- apply_filters()Calls the callback functions that have been added to a filter hook.
- _cleanup_header_comment()Strips close comment and close php tags from file headers used by WP.
Used by · 5
- Theme_Upgrader::check_package()Checks that the package source contains a valid theme.
- WP_Theme::__construct()Constructor for WP_Theme.
- WP_Theme::get_block_patterns()Gets block pattern data for a specified theme.
- get_plugin_data()Parses the plugin contents to retrieve plugin's metadata.
- wp_get_pomo_file_data()Extracts headers from a PO file.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.