Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am looking for something very simple but I'm not quite sure how to achieve this. I have been researching for the past 2-3 hours and gathered a lot of useful information. However, my knowledge in both PHP and WordPress isn't enough to do this from scratch.

What I need is the following:

  • When the user is not logged in, this HTML code should be displayed:

    <a href="#">Login</a>
    
  • When the user is logged in, the previous HTML code should be replaced with:

    <span>Welcome, *username*</span>
    <a href="actual-link-for-logging-out-the-user">Logout</a>
    

Could anyone please help me out. I'd really appreciate the help :)

Thanks

share|improve this question
Those messages should be displayed where? – s_ha_dum Jan 14 at 5:04

1 Answer

up vote 1 down vote accepted

You're going to want to use the is_user_logged_in() conditional tag to handle this.

global $current_user;
get_currentuserinfo();

if ( is_user_logged_in() ) {
  echo "<span>Welcome $current_user->user_login</span>".
       "<a href='".wp_logout_url()."'>Logout</a>";
} else {
  echo '<a href="#">Login</a>';
}

Other functions referenced: wp_logout_url() & get_currentuserinfo()

FYI, all of this could easily be found on Google or in the WordPress Codex.

share|improve this answer
+1 for the Answer as a whole, -0.5 for the FYI. Especially for beginners in a tech (like PHP or WordPress), it's not so easy to craft the correct search terms to find an answer to your problem. – akTed Jan 14 at 3:44
@AKTed: Give a man a fish, and all of that... – Dan Jan 14 at 4:40
1  
Hey Dan, thanks a lot! You're awesome! That works perfectly and was exactly what I was looking for. – Kaneda Jan 15 at 16:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.