WordPress Tutorial: How To Display A User Welcome Message




I believe that creating a great user interaction with your blog or website is key in keeping return visitors and helping with word of mouth advertisements and link love.

I know I’m not alone in this either because on almost every blog about marketing, blogging, and tutorials you find that the owners have added all of the extra plug-ins for their comments section, they’ve added in the “top commentators” and “most commented” plug-ins and sometimes use the LinkLove plug-in that will remove the nofollow attribute on commentators names after X amount of posts. So, what else is there that almost all blogs are overlooking that could help put them one step closer to creating the perfect user interaction?

Displaying A User Welcome Message

Some time ago I was reading a post over at WP Candy and it caught my eye. It was a tutorial on how to display content only to the site admin. This got me thinking and I even posted a quick reply there with a brief idea of what I’d like to do here today. I’m going to adopt the code from WP Candy, but please make sure you check out their website as well for the original article along with tons of other useful information.

First, we’re going to need to display the code below which will make sure the logged-in user is a level 0 which is the default level for members who have no power over anything other then leaving comments. This also allows any user level above 0 to see their username as well (ie: administrator level is 10, but they can still see this message as well). This also works well if you want to display a message for only a certain level of visitor (ie: admin, contributor, etc…).

< ?php global $user_ID; if( $user_ID ) : ?>
< ?php if( current_user_can('level_10') ) : ?>
< a href="http://www.yoururl.com/stats">Stats< /a>
< ?php else : ?>
< ?php endif; ?>
< ?php endif; ?>

Now, you can see the link code in the middle of this which links to a “stats” page. We’re going to remove that line and replace it with the code below which was found in the WordPress Codex. This code will display the name of the user who is currently logged into your website.

< ?php global $current_user; get_currentuserinfo(); echo ($current_user->user_login); ?>

Now that we have the new snippet of code added in, the full code should look like this:

< ?php global $user_ID; if( $user_ID ) : ?>
< ?php if( current_user_can('level_10') ) : ?>
< ?php global $current_user; get_currentuserinfo(); echo ($current_user->user_login); ?>
< ?php else : ?>
< ?php endif; ?>
< ?php endif; ?>

From here, you can modify what’s shown when the code echoes. Just add text before and after the echo code with any welcome message you’d like. For instance, I’ve added a couple full code snippets below which range from basic “Welcome back username” messages to a full blown welcome back message with links to various other pages of the site.

Simple Welcome Back Message

< ?php global $user_ID; if( $user_ID ) : ?>
< ?php if( current_user_can('level_0') ) : ?>
	
Welcome back < ?php global $current_user; get_currentuserinfo(); echo ($current_user->user_login); ?>
< ?php else : ?>
< ?php endif; ?>
< ?php endif; ?>

Welcome Back Message With An “in case you’ve missed these” Message And 5 Recent Posts

< ?php global $user_ID; if( $user_ID ) : ?>
< ?php if( current_user_can('level_0') ) : ?>
	
Welcome back < ?php global $current_user; get_currentuserinfo(); echo ($current_user->user_login); ?>. In case you’ve missed anything, here’s what we’ve recently posted.

<ul>< ?php wp_get_archives('type=postbypost&limit=5'); ?></ul>
< ?php else : ?>
< ?php endif; ?>
< ?php endif; ?>

Obviously for the above code, you can add a class or id to your ul tags or style them completely different. That’s totally up to you, but as you can see, the possibilities are unlimited. Below is a screenshot of how I’ve implemented this into my personal blog for the users who register to see a welcome back message.

The Welcome Back Admin is wrapped in an H3 tag and the style of the list follows the styling of the other lists in my sidebar.

wp-welcome-message

Your Turn To Talk

Feel free to expand on this and let me know in a comment if you have. I hope everyone enjoyed this tutorial. I am new to writing up tutorials like this, so bare with me if I’m not being in detail enough. If you have questions for me, feel free to leave a comment and I will reply to it for you. :)


Comments:

Scroll back to the top
Content


Receive the top stories from SpyreStudios and the Splashpress Media network every week, right in your Inbox. Relevant and timely content is yours for FREE!