How to access User meta data within a plugin

Please look at the last two lines of code. I am trying to print the id of the user. It works when used in the theme files but not inside the plugin.

//Test Usermeta

$user = get_userdatabylogin('vijay');
echo $user->ID; // prints the id of the user;

In PHP, you cannot (normally) execute a user function before it has been defined.

The plugin is executing the get_userdatabylogin() code before get_userdatabylogin() has been defined as a function.

The theme is executing the get_userdatabylogin() code after get_userdatabylogin() has been defined as a function.

Consult the Plugin API (or a WP Hooks database or search the WordPress code) to find the correct action name. Since you already know the code works in the theme, you should be able to use the after_setup_theme action to run your code.

add_action( 'after_setup_theme', 'test_user_meta' );
/**
 * Test User meta.
 */
function test_user_meta() {
    $user = get_userdatabylogin('vijay');
    echo $user->ID; // prints the id of the user;
}