Code Snippet

Home » Code Snippets » WordPress » Login/Logout and User Welcome

Login/Logout and User Welcome

<div id="user-details">
<?php
   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 } ?>
</div>

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

Subscribe to The Thread

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

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

      TQ…

  2. This doesnt seem to work, breaks the whole site!

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

    Cheers!

  3. 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. ug, tic mark was converted when I posted
    '

  5. yop! ..usefull! thx

  6. Nice tutorial.. thanks for that.

  7. 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. sorry it’s 500 not 505 Internal Error

    The message is:

    “0

    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. 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. Aron

    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. 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:

    <?php
       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. nope ok visit

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

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~