Mar
6th

WordPress: Check if User is Logged In

Thanks for stopping by my personal blog on Marketing Technology! Over 50,000 visitors a month find my content worth returning for, so don't forget to subscribe to the Marketing Technology Blog RSS feed or to the Marketing Technology Email to have new content sent directly to your inbox. You may also find my other business blog helpful, Social Media Domination.

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:

<?php
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!

RSS feed | Trackback URI

5 Comments »

Comment by no imageMartin (SezWho)
2007-03-07 11:37:14

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

Rate this:
2.9
Comment by no imageDouglas Karr (SezWho)
2007-03-08 09:03:12

Thanks so much Martin! I was redirecting to the wrong page! Oops! I wasn’t going to get too many referrals that way, was I? :)
Rate this:
2.9
 
 
2007-03-07 19:36:04

[...] Check if User is Logged In - Nice little script for WordPress bloggers - changes the options in the Header, according on who’s reading. [...]
 
Comment by no imageAllstar (SezWho)
2007-04-15 00:41:03

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”;
endif;

Rate this:
1.6
 
Comment by no imageVlad (SezWho)
2008-03-08 05:07:58

Actually WordPress provides an easier way to check if the used is logged in or not, a functions implemented in WP by default: is_user_logged_in()
Rate this:
1.6
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

My Comment Policy: I moderate comments. Please be patient:

  • Spam will happily be destroyed.
  • Use your real name, not some keywords. Otherwise it will be destroyed.
  • Mean comments aren't necessary. If I don't post them I will reply personally to let you know why.
  • Lewd comments will be edited, I don't want my readers leaving because of offensive content.
Great debate, criticism and colorful commentary is always appreciated and approved!