How WordPress reading comment lines

2020-02-29 01:05发布

问题:

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.

回答1:

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' );


标签: php wordpress