How to display logged in username in Wordpress
For whatever reason you may want to have users register to your Wordpress blog to make them login. Whether it’s to protect certain content, only allow comments from logged in users, etc., it’s makes your website look a little bit more professional if you can display a login button when they are not logged in and display a logoff button when they are logged in just like you see at the top of this website. It’s an even better touch when you can display their name after they login. This blog is going to show:
- How to display a login button on Wordpress that doesn’t send users to the backside of Wordpress
- How to display the logged in users first and last name
Step1
The default login page for Wordpress is http://yourdomain.com/wp-admin. When you login to Wordpress using this URL it immediately takes you to the backside of Wordpress (not very user friendly). The URL to allow a user to login and be sent back to the home page of your website is
and the URL to allow users to logout and be sent back to the home page is
Step2
Now that the user can login/logout, all that’s left is getting their First and Last name. The code to get their name is
get_currentuserinfo();
echo($current_user->user_firstname . ” ” . $current_user->user_lastname . “, “);
?>
Conclusion
Now, to put all that together is
<p style=”text-align: right”><?php if ( is_user_logged_in() ) { ?>
Welcome:
<?php global $current_user;
get_currentuserinfo();
echo($current_user->user_firstname . ” ” . $current_user->user_lastname . “, “);
?>
<a href=”<?php echo wp_logout_url(); ?>&redirect_to=/”>Logout</a>
<?php } else { ?>
You are not logged in –> <a href=”/wp-login.php?redirect_to=/”>Client Login</a>
<?php } ?>
</p>
</div>
Of course, you can use any div name that you want instead of “menutop”. Below is the CSS that I used for my div
font-size: 8pt;
color: #ffffff;
float: right;
}
BTW…I placed all of this in my header.php file.
Filed Under: Wordpress



How bizarre – I’ve literally just sent off a proposal for a membership WP site, and this will be of massive help.
There is actually no need to write your own login url either. WP has a function for that. “wp_login_url()”. Also, both the login_url and logout_url functions give you the option to pass in the redirect parameter, so you don’t need to append it to the end of the function.
So your login would look like:
wp_login_url(“/”);
And logout would look like:
wp_logout_url(“/”);
Hope that helps.
Great! I hope this helps.
I didn’t know that…thanks for the additional info!
A minute’s success pays the failure of years.