在WordPress中,注释行用于查找主题摘要,插件摘要,模板名称等.
例如:-
<?php
/*
Template Name: Snarfer
*/
?>
Run Code Online (Sandbox Code Playgroud)
WordPress如何做到这一点?用什么代码来读取注释行.
这是在函数get_file_data中完成的,wp-includes/functions.php关键代码部分是这样的:
foreach ( $all_headers as $field => $regex ) {
preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, ${$field});
if ( !empty( ${$field} ) )
${$field} = _cleanup_header_comment( ${$field}[1] );
else
${$field} = '';
}
Run Code Online (Sandbox Code Playgroud)
例如,对于插件,它wp-admin/includes/plugin.php在函数中引用get_plugin_data:
$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
Run Code Online (Sandbox Code Playgroud)