Login/Logout and User Welcome

<div id="user-details">
   if (is_user_logged_in()) {
      $user = wp_get_current_user();
      echo ‘Welcome back <strong>’.$user->display_name.‘</strong> !’;
   } else { ?>
      Please <strong><?php wp_loginout(); ?></strong>
      or <a href="<?php echo get_option(’home’); ?>/wp-login.php?action=register"> <strong>Register</strong></a>
<?php } ?>

Uses WordPress functions and queries to pull user information, and to display the login/logout/register links.


  1. User Avatar
    Permalink to comment#

    thanks for this…
    this code very usable for multi authored site..

    • User Avatar
      Permalink to comment#

      Please add some additional code for integrate gravatar with login user….


  2. User Avatar
    Permalink to comment#

    This doesnt seem to work, breaks the whole site!

    Any ideas? Is there any other config that needs to be done?


  3. User Avatar
    Permalink to comment#

    I didn’t test this code, but the single quotes might be breaking the code. They may need to be replaced with the regular tic mark ( ‘ ) instead of the fancy single quotes ( ‘ ) and ( ’ ), which probably were converted when copy/pasting from ms Word

  4. User Avatar
    Permalink to comment#

    ug, tic mark was converted when I posted

  5. User Avatar
    Permalink to comment#

    yop! ..usefull! thx

  6. User Avatar
    Steward John
    Permalink to comment#

    Nice tutorial.. thanks for that.

  7. User Avatar
    Permalink to comment#

    hello, I tried to do this but it didn’t work and now I can’t even log in to wordpress nor my website. It gives me a 505 internal error message and tell me to contact “cgiadmin@yourhostingaccount.com”. Please advice.

  8. User Avatar
    Permalink to comment#

    sorry it’s 500 not 505 Internal Error

    The message is:


    500 Internal Server Error

    The server encountered an internal error or misconfiguration and was unable
    to complete your request.

    Please contact the server administrator,
    cgiadmin@yourhostingaccount.comand inform them of the time the error”

  9. User Avatar
    Permalink to comment#

    i need to put this code where cause i didn’t find the location :( am i need to create one more page ? or i need to put this code in a wadge ? please tell me

  10. User Avatar
    Permalink to comment#

    Thanks for this, works well for me.

    My only question is how to extend the Logged in message to include an option to log out??

    i.e, Welcome Back USER, ‘logout?’

    Many thanks

  11. User Avatar
    Andrew @ EmailExpert
    Permalink to comment#

    Thanks tristan

    Aron the code your looking for (complete) is here, it also offers a hardcoded link to where you like (replace EMailExpert.org) by clicking username:

       if (is_user_logged_in()) {
          $user = wp_get_current_user();
          echo 'Hi <strong><a href="http://emailexpert.org/" rel="nofollow">'.$user->display_name.'</a></strong> | <a href="">Logout</a>';
       } else { ?>
          Please <strong></strong>
          or <a href="/wp-login.php?action=register"> <strong>Register</strong></a>
  12. User Avatar
    Andrew @ EmailExpert
    Permalink to comment#

    nope ok visit

    http:// codex.wordpress.org/ Function_Reference/ wp_logout_url

  13. User Avatar
    Asif Irtiza
    Permalink to comment#

    Hi, thanks a lot for this code. I edited it a little and used it on my site. I wanted to share the code but i could not write that code in the comment form.

  14. User Avatar
    Permalink to comment#

    Where will you paste this code? Need Help here

  15. User Avatar
    Permalink to comment#

    This is very useful to me, gonna try it now. thnx !!!

  16. User Avatar
    Permalink to comment#

    Where i have to put that code?? In function.php ?

  17. User Avatar

    this is my php code in login page, when i login it gives me this message.
    Welcome root, please help me

    echo "welcome ".$_SESSION['use'];

    header("location: logg.php");

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.