WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Getting user id from username? (9 posts)

  1. Rolle
    Member
    Posted 5 years ago #

    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

  2. Austin Matzko
    Member
    Posted 5 years ago #

    I think you want get_userdatabylogin():

    $user = get_userdatabylogin('myusername');
    echo $user->ID; // prints the id of the user
  3. Rolle
    Member
    Posted 5 years ago #

    Does not seem to work. Nothing prints out.

  4. Rolle
    Member
    Posted 5 years ago #

    Bump. Any clue?

  5. Austin Matzko
    Member
    Posted 5 years ago #

    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).

  6. Rolle
    Member
    Posted 5 years ago #

    So how? Like this?:

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

    ...which prints out NULL.

  7. Austin Matzko
    Member
    Posted 5 years ago #

    $user = get_userdatabylogin('admin');
    var_dump($user);
    echo $user->ID; // prints the id of the user
  8. Rolle
    Member
    Posted 5 years ago #

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

  9. jlyon
    Member
    Posted 5 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic