3

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

/*
    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);

1 Answer 1

5

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];
6
  • please see my own answer as i couldn’t figure out how to put code in a comment xXx
    – Sofia Rose
    Jun 9, 2013 at 22:53
  • @SofiaRose See my update. You cannot read a directory, it must be a real file.
    – fuxia
    Jun 9, 2013 at 22:55
  • so i cant read a file in another directory?
    – Sofia Rose
    Jun 9, 2013 at 22:56
  • Of course it can. But the path must be correct. Try file_exists() to verify it is correct.
    – fuxia
    Jun 9, 2013 at 22:58
  • sorry i was having a stupid moment, i needed dirname(file).'/some-file.php' not dirname('some-file.php')
    – Sofia Rose
    Jun 9, 2013 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.