WordPress Tip: Function is_user_logged_in

A PHP function is available in WordPress called is_user_logged_in that is very useful if you want to know if a user is logged in.  Why on earth would you want to know this?  Have you ever thought about adding custom content to your blog that only you can see or only logged in (registered users) can see?

If so this function is for you.  I recently updated my blog’s theme and realized that it has some space that could be used for secondary navigation.  nolinks1 The area in red seemed to me like a great place for additional links.

This is where the function is_user_logged_in can help me out.   In this case the area highlighted in red has its own div tag.  It currently looks like this:

<div id=”nav-space”></div>

Inside the div tag I place the following 2 lines.

<?php if (is_user_logged_in()){ ?>
<?php } ?>

The 2 lines call the function is_user logged_in and evaluate the return value with my if statement. The second statement simply closes or ends the if. Do not worry if you do not understand.  Let me just tell you what it does.

Suffice it to say that anything you put between the 2 lines will be seen by logged in users only. For this example I am going to insert a link.

<div id=”nav-space”>
<?php if (is_user_logged_in()){ ?>
<a href=”http://www.google.com”>Google</a>
<?php } ?>

In the example just above a link to Google will be placed in the red area above that can only be seen by a logged in user.  In this case, me!

Now let’s put it in to practice.    I have added 3 links to the red bar section.  I now have access to facebook, friendfeed, and twitter right in my header section. I like this.  I think I will add some additional links later.

Now remember they will only display if you are logged in.  If you are like me you mainly use the same computers and stay logged in.  As long as you are logged in you will see your links.

Do you hang out on your own blog alot?  Would you like to use your own blog for a mini portal? Do you have free navigation space available?

This is great for me and adds functionality for my purposes but leaves the blog crisp and clean for visitors.  Try this WordPress trick in your theme today.