WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Getting user id from username?

[Resolved] Getting user id from username?

  • I know how to get user information from user id, but how to get user id from username? I tried something like $userid(user) but no joy. I need this because I’m trying to add gravatrs to wp-useronline -plugin which has username implementation but not user email. If this is not possible, then is it possible to get user email from username?

    Thanks in advance.

    Rolle

Viewing 8 replies - 1 through 8 (of 8 total)
  • I think you want get_userdatabylogin():

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

    Does not seem to work. Nothing prints out.

    Bump. Any clue?

    Do var_dump($user); to see if any data is being returned from get_userdatabylogin. If myusername is the user name of a user, it will work, unless a plugin has redefined get_userdatabylogin so that it doens’t work (unlikely).

    So how? Like this?:

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

    …which prints out NULL.

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

    Thanks. That worked fine for me.
    I owe you one 😉 Happy new year 2009.

    Thank you thank you. I was looking for ever to find a means of extracting the userid from login name and your snippet worked a treat!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] Getting user id from username?’ is closed to new replies.