WordPress Conditional Tag: is_user_logged_in

July 20, 2010 No comments yet

On the welcome screen of a recent web project, I was asked to create a function to allow visitors to receive a free audio file after they registered on a WordPress powered site. Now I’m sure there are a number of plugins that can easily do this task, however, I wanted to let WordPress do all the work. Right away I thought of the Template Tag – “is_user_logged_in”. The function is rather simple, it checks to see if the current visitor is logged in or not. Now here’s where you can have some fun and get your visitors to actually become registered members of your site.

So on the index page I created a very simple function:


<?php
if ( is_user_logged_in() ) {
echo 'Here is your free audio file! ';
echo '<a href="\http://www.yoursite.com/files/audio.mp3"\> FREE FILE!</a>';
} else {
echo 'Register to this blog today and receive a FREE audio file!';
}
?>

Rather simple. We are seeing if the current visitor is logged in, if so, give them their free gift, if not, ask them to register!

Now, of course, WordPress makes it possible to use this just about where ever you would want. Want to display a message to only users logged into your site AND are viewing the Contact page? Try this.


<?php
if (is_page('Contact') && is_user_logged_in()) {
echo 'Hello Registered Member, here are the latest updates!';
} else {
echo 'Hello Visitor, Please register for complete updates!';
}
?>

Of course, there is much much more that you can do with this Template Tag. You could even use this Template Tag within Sessions (view a welcome message during first page load, etc.).

Have any suggestions on how to further use Template Tag: is_user_logged_in()