get_user()
2010-01-16 @ 14:51The problem
WordPress have a built in function called get_currentuserinfo. It’s can be used like this example.
global $current_user; get_currentuserinfo(); 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; get_currentuserinfo(); switch ($type) { case 'ID': return $current_user->ID; break; case 'displayname': return $current_user->display_name; break; case 'username': return $current_user->user_login; break; case 'firstname': return $current_user->user_firstname; break; case 'lastname': return $current_user->user_lastname; break; case 'level': return $current_user->user_level; break; case 'email': return $current_user->user_email; break; default: return; } }
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.
Improvements
Do you have any ideas, bugs, features or anything else to improve the code? Write a comment and I’ll look into it.
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 ;) )
2010-01-17 @ 2:32 e m
Try this:
echo get_user(‘email’);
2010-02-01 @ 3:31 f m
Thanks man… this was just what i was looking for…
:)
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! :)