Loading...

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;
}

Latest Posts

Portfolio

Contact

Fill in the form below to send us a message. Your details will not be used for promotional purposes or passed on to any third parties.

 

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close