WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Christiaan Conover
    Member
    Posted 8 months 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
    Member
    Posted 8 months 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
    Member
    Posted 8 months 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
    Member
    Posted 8 months 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!

Reply

You must log in to post.

About this Topic