Get User Name

August 27th, 2008

Sometimes you might need to check if a current user is logged in or not within your theme. This is a way to accomplish that issue.
I have created a function to get the userdata as a returned string value. I recommend you to put in the functions.php file, placed within your current theme folder.

function wt_get_user_name()
{
	global $userdata;
	get_currentuserinfo();
	return $userdata->user_login;
}

Now you can print it out to see that you get a string value (this will only work when you are logged in). Put the code below somewhere in your theme to do so.

<?php echo 'Welcome ' . wt_get_user_name(); ?>

To check if a specific user is logged in try this:

<?php
if(wt_get_user_name() == 'Adam')
{
	echo 'You have all the rights to visit this page!'
}
else
{
	echo 'You do not have permission to view this page!'
}
?>

To get all new functions/template tags, install my plugin WP Extra Template Tags.

  1. 2010-08-12 - បាត់ដំបង

    Thanks so much

  2. 2009-07-15 - Voya

    Thanks a million. I was wondering how can i “echo” the username, once someone is logged in.