函数原型
get_file_data( string $file, array $default_headers, string $context = ” ): string[]
函数描述
Retrieves metadata from a file.
是否弃用
未弃用
函数参数
-
$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:
''
函数返回值
string[] Array of file header values keyed by header name.
函数位置
File: wp-includes/functions.php.
函数源码
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;
}
源码链接
变更日志
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |

