Get file headers in custom file

in my one of my plugins i have modules (kind of like plugins in a plugin), is there a function like get_plugin_data() or wp_get_theme() that will allow me to get the header section of a custom file (by passing the path as a parameter)?

By heading section i mean

Read More
/*
    Plugin Name: Name Of The Plugin
    Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
    Description: A brief description of the Plugin.
    Version: The Plugin's Version Number, e.g.: 1.0
    Author: Name Of The Plugin Author
    Author URI: http://URI_Of_The_Plugin_Author
    License: A "Slug" license name e.g. GPL2
*/

Thanks to @toscho the correct solution was

$default_headers = array(
    'Module Name' => 'Module Name',
    'Test Header' => 'Test Header',
    );

$file_data = get_file_data(dirname(__file__).'/some-file.php', $default_headers);

print_r($file_data);

Related posts

1 comment

  1. Use get_file_data( $file, $headers ):

    $file_data = get_file_data( __FILE__, array ( 'Plugin Name' ) );
    
    echo "the name is " . $file_data[0];
    

    Make sure the first parameter points to an existing file.

    It will find all lines that are formatted like regular plugins headers or the headers of a style.css.

    In my plugin T5 Opera Speed Dial Preview I use it to show the link to my bug tracker:

    $data = get_file_data( __FILE__, array ( 'Feedback URI' ) );
    return empty ( $data ) ? '' : $data[0];
    

Comments are closed.