2010-01-16 @ 14:51

The problem

WordPress have a built in function called get_currentuserinfo. It’s can be used like this example.

global $current_user;
echo $current_user->user_login;

A global variable is set. It calls a function to get the user information. It then echos the username. The code is not as short and easy as it could be.

The solution

I created a function that returns a value depending of the input type. Put the function into your functions.php in your theme folder.

function get_user($type = 'ID')
     global $current_user;

     switch ($type)
          case 'ID':
               return $current_user->ID;
          case 'displayname':
               return $current_user->display_name;
          case 'username':
               return $current_user->user_login;
          case 'firstname':
               return $current_user->user_firstname;
          case 'lastname':
               return $current_user->user_lastname;
          case 'level':
               return $current_user->user_level;
          case 'email':
               return $current_user->user_email;

The function call

Call the function inside the post loop somewhere in your theme. The example below prints out all the current user information.

echo get_user('ID');
echo get_user('displayname');
echo get_user('username');
echo get_user('firstname');
echo get_user('lastname');
echo get_user('level');
echo get_user('email');

If the input type is not sent it will return the category ID, as default.


Do you have any ideas, bugs, features or anything else to improve the code? Write a comment and I’ll look into it.

RSS-feed for comments

4 replys to “get_user()”

  • nicolai
    2009-09-26 @ 2:30 e m

    Hey, i’ve got a problem with ur function…
    When i use the functon my site will not show me anything…

    Also I used get_currentuserinfo();
    .. But there was the same problem…

    Do You know, why i have so big problems?

    (and yes, i put the function in the function.php of my theme ;) )

  • Chris
    2010-02-01 @ 3:31 f m

    Thanks man… this was just what i was looking for…


  • Harlow
    2010-08-04 @ 6:51 e m

    Hi, this is a great piece of code, thank you! :)

    Since you say if we have any ideas we can write a comment and you’ll look into it, I have an idea.

    Would it be possible to adapt your code and show, only on the dashboard of the admin, who is logged (nickname)? That would be very useful and I wish I could do it by myself but unfortunately, I do know how to code that…

    I hope you will into it, thank you! :)

Leave a reply