Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
share|improve this question

1 Answer 1

up vote 3 down vote accepted

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];
share|improve this answer
    
please see my own answer as i couldn’t figure out how to put code in a comment xXx –  Sofia Rose Jun 9 '13 at 22:53
    
@SofiaRose See my update. You cannot read a directory, it must be a real file. –  toscho Jun 9 '13 at 22:55
    
so i cant read a file in another directory? –  Sofia Rose Jun 9 '13 at 22:56
    
Of course it can. But the path must be correct. Try file_exists() to verify it is correct. –  toscho Jun 9 '13 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 '13 at 23:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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