WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on creating some text that says 'Login' to users that are not logged in, and the user's username or display name when logged in.

It seems like it should be an easy problem to solve, and I've found the following two bits of code on the wordpress codex that each do half of what I am looking for, but I haven't figured out how to combine them (without breaking the site).

Is this the correct direction, or way off base?

To check if the user is logged in and display something different depending:

<?php if ( is_user_logged_in() ) {
    echo '{username code here}';
} else {
    echo 'Login';

To get and display the current user's information:

<?php global $current_user;
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
share|improve this question
up vote 2 down vote accepted

This seems to do what you need.

<?php global $current_user; get_currentuserinfo(); ?>
<?php if ( is_user_logged_in() ) { 
 echo 'Username: ' . $current_user->user_login . "\n"; echo 'User display name: ' . $current_user->display_name . "\n"; } 
else { wp_loginout(); } ?>
share|improve this answer
Awesome, worked like a dream. Thanks! – Stephen S. Apr 20 '12 at 18:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.