How to: get main plugin/theme file?

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 Comment