WordPress: Check if User is Logged In

John Chow had a nice tip on his site today on posting an advertising link on your site and ensuring that it’s not indexed using nofollow. You can do this with a simple redirect page that John posts the code for.

I wanted to do this a little different. In my horizontal menu above, real estate is at a premium. I do have an admin link on there if I’m logged in… but for everyone else, that’s a blank spot. I decided, why not swap the content of that bar in the event it’s a visitor and not me? With WordPress, this is pretty simple:

get_currentuserinfo() ;
global $user_level;
if ($user_level > 0) {
wp_register('>li class="menuitem">','>/li>');
} else {
echo ">li class='menuitem'>>a href='/go/tla.html' title='Advertise on this Website'>Advertise>/a>>/li>";

I haven’t made a dime with Text Link Ads yet but I want to give it a good shot. It’s pretty inexpensive for my site… $35 per month and I get over 500 visitors a day. I look forward to serving some of these up!


  1. 1

    Cool idea!

    Though I would I would revise the link it points to – now it asks you for your log-on details. Not the kind of page I wanted to be greeted with, if I were a prospective advertiser 😉

  2. 3

    Surely this piece of code would be more efficient and have similar effect?

    /* If a browser has a user ID they must be logged in */
    if ( $user_ID ) :
    echo "I am logged in";
    /* For everybody that doesn't have one */
    else :
    echo "I am logged out / not logged in";

  3. 4
  4. 5

    <?php global $user_ID; if( $user_ID ) : ?>
    <?php if(current_user_can('level_10')) : ?>
    <!– Things to be displayed for admon –>
    <?php endif; ?>
    <?php endif; ?>

    this will help

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.