Forums

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

  1. Rolle
    Member
    Posted 10 months 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. filosofo
    Member
    Posted 10 months ago #

    I think you want get_userdatabylogin():

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

    Does not seem to work. Nothing prints out.

  4. Rolle
    Member
    Posted 10 months ago #

    Bump. Any clue?

  5. filosofo
    Member
    Posted 10 months 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 10 months 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. filosofo
    Member
    Posted 10 months ago #

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

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

  9. jlyon
    Member
    Posted 6 months 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!

Reply

You must log in to post.

About this Topic