[resolved] get_plugin_data() is undefined function? (4 posts)

  1. Christiaan Conover
    Posted 1 year ago #

    I have the following code in my plugin's main file:

    $_ENV['cc_author_plugindata'] = get_plugin_data( __FILE__, false );

    I get the following error:

    Fatal error: Call to undefined function get_plugin_data()

    I'm inside wp-admin, so I'm not sure why this function isn't available. What's going wrong?

  2. bcworkz
    Posted 1 year ago #

    Plugins are loaded fairly early and WP isn't fully functional yet. You probably need to use the function from within an action callback that fires later in the process. If you can wait that long, 'init' should work.

  3. Christiaan Conover
    Posted 1 year ago #

    Oh, I'm following you. Yeah, I'm calling get_plugin_data() immediately in the script.

    I'll try finding a place further down to place it that will still allow me to get it when I need it.

  4. Christiaan Conover
    Posted 1 year ago #

    It's working now! I moved the call into my main admin file, and put it inside a function called by the 'admin_init' hook. Here's the code:

    function cc_author_admin_init() {
    	/* Set plugin version data for use elsewhere in the plugin */
    	if ( function_exists( 'get_plugin_data' ) ) {
    		$_ENV['cc_author_plugindata'] = get_plugin_data( plugin_dir_path( dirname( __FILE__ ) ) . 'author-customization.php', false );
    	else { // If the function get_plugin_data does not exist, return empty array
    		$_ENV['cc_author_plugindata'] = array(
    			'Version'	=>	''
    add_action( 'admin_init', 'cc_author_admin_init' ); // Hook plugin admin initialization

    Thanks for the help!

Topic Closed

This topic has been closed to new replies.

About this Topic