8

I have a class inside a file that i include_once() from the functions.php of my theme. The class can be used as stand alone plugin, or integrated in a theme.

Inside the class i want to fill a class variable (inside the __construct() function) with the native wordpress function get_plugin_data(). Fun as it is, i get the following error:

Call to undefined function get_plugin_data()

Am I asking for the plugin data too late, or what else might be my problem?

Thanks!

5 Answers 5

9

get_plugin_data() is only defined in the administration section. What info did you need to get from the plugin's header that you would need to display in the theme? If the answer is none, I would suggest that you place a conditional call in your constructor. Something like:

if ( is_admin() ) {
    $var = get_plugin_data();
}
1
  • 1
    When you are developing a plugin, it is useful to get plugin's version automatically from the plugin's header, and insert it as a parameter of functions like "wp_enqueue_style" or "wp_enqueue_script", for cache busting purposes. Feb 19, 2021 at 18:39
13

If you need to use the get_plugin_data on the frontend, you can add this code before calling the function:

if( !function_exists('get_plugin_data') ){
    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
2
  • Shouldn't this be the correct answer? It should be used in combination with is_admin...
    – tver3305
    Oct 11, 2018 at 4:31
  • 1
    Did the trick for me - thanks! I'm using the plugin version as the version for the plugin stylesheet - so it busts the cache
    – Dave
    May 30, 2019 at 18:16
8

You might wonder is there another way to solve this? At least that's what I did, because the solution @mfields posted didn't work for me.

After core code digging I found an pretty useful core helper function which will help us achieve about the same result. I'm talking about the function get_file_data().

As first argument we need a string of the absolute path to your main plugin file. The second argument is an array of the headers you want to retrieve, check the array below for all headers.

$default_headers = array(
    'Name' => 'Plugin Name',
    'PluginURI' => 'Plugin URI',
    'Version' => 'Version',
    'Description' => 'Description',
    'Author' => 'Author',
    'AuthorURI' => 'Author URI',
    'TextDomain' => 'Text Domain',
    'DomainPath' => 'Domain Path',
    'Network' => 'Network',
    // Site Wide Only is deprecated in favor of Network.
    '_sitewide' => 'Site Wide Only',
);

The third argument is going to be "plugin", don't tell me why without third argument it works as well.

Anyway this is what I use now (it's located in the main plugin file).

$plugin_data = get_file_data(__FILE__, [
    'Version' => 'Version',
    'TextDomain' => 'Text Domain'
], 'plugin');
1

Building on @mfields and @gleb-kemarsky answers, if the plugin is to be used by the admins, we need to check whether the plugin is being called by an admin first

if ( is_admin() ) {
    if( !function_exists('get_plugin_data') ){
        require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    }
    $plugin_data = get_plugin_data( __FILE__ );
}
1
  • Note: is_admin() does not check whether the plugin is being called by an admin, it checks if the request is for an admin page, regardless of permissions, e.g. subscriber.
    – Andy Gee
    Mar 6 at 18:02
0

you can get the value of the relevant variable from the following code without any problem

//define this plugin  language
    defined('define_name') or define('define_name', get_file_data(__FILE__, ['Lang' => 'Text Domain'], 'plugin')['Lang']);
1
  • I don't think #define is valid PHP.
    – Rup
    Oct 18, 2020 at 10:40

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.