How WordPress reading comment lines

2020-02-29 01:19发布

In WordPress, comment lines are used to find theme summary, plugin summary, template name and so on.

for example:-

<?php
/*
Template Name: Snarfer
*/
?>

How WordPress doing this? What code is used to read comment lines.

标签: php wordpress
1条回答
劳资没心,怎么记你
2楼-- · 2020-02-29 01:54

This is done in the function get_file_data in wp-includes/functions.php with the key code section being this:

    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} = '';
    }

For example for a plugin it is referenced in wp-admin/includes/plugin.php in the function get_plugin_data:

$plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
查看更多
登录 后发表回答