Here’s another little function that does a simple check on the wordpress user_id and returns
“Welcome back, UserDisplayName”
when a user is logged in (linking the username to their profile page), and shows
“You are currently logged out. Please Log In”
when a user is logged out, with Please Log In linking to the wp-login page.

So here is the function, copy and paste into the current theme’s functions.php file.

function nnm_showuser(){
	 $nnm_user = wp_get_current_user();
	 $nnm_user = $nnm_user->exists() ? 'Welcome Back, <a href="./profile.php">' . $nnm_user->display_name . '</a>' : 'You are currently logged out, <a href="./wp-login.php">Please Log In</a>';
	 echo '<div id="nnm_showuserbox">' . $nnm_user . '</div>';

You may need to edit the two links, to ensure they are going to the correct places. Look for the urls: ./profile.php and ./wp-login.php and change them to the correct links for your site. (./profile.php is the link for the output when the user is logged in).

Here is the function call, I pasted this in the header.php to display my link above the navigation bar.

<?php nnm_showuser(); ?>

And finally the CSS to tidy up the box, you can edit this as you require

#nnm_showuserbox {
  width: 33%;
  float: right;
  clear: left;
  margin: 0 20px 10px 0;

One response to “WordPress Logged in as / Log In function

Leave a Reply

Privacy & more..

Cookie Consent Policy