There’re the functions get_theme_data(); & get_plugin_data(); – which both use get_file_data(); that needs a specific file as $input.

I got a set of classes that may be used by a plugin or a theme, without a specific location. Everything works, but I don’t know how i would determine what the main plugin or theme file is – I mean the one containing the comment header that holds the information (Version, Author, etc.) about the Theme/Plugin.

3 Answers
3

You can get theme’s main file using following:

$theme_info_file = trailingslashit( get_template_directory() ) . 'style.css';

To get the plugin’s main file you need to know the plugin name. And then you can use following function:

function get_plugin_file( $plugin_name ) {
    require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
    $plugins = get_plugins();
    foreach( $plugins as $plugin_file => $plugin_info ) {
        if ( $plugin_info['Name'] == $plugin_name ) return $plugin_file;
    }
    return null;
}

For example to get the plugin main file for Akismet.

$akismet_plugin_file =  trailingslashit( WP_PLUGIN_DIR ) . get_plugin_file( 'Akismet' );

Leave a Reply

Your email address will not be published. Required fields are marked *