0

I am trying to display a logout link when users are logged in and make it disappear when they are not logged in.

The code I have so far works for the login and register links due to a plugin that I have installed, so 'login' and 'register' disappear just fine, now for logout I need some assistance... Here is the code:

<ul id="login-navigation">
<?php if ( is_user_logged_in() ) {
          $user = wp_get_current_user();
          echo 'Welcome <strong>'.$user->display_name.'!</strong> ';
} else { ?>
    <a href="/wp-login.php?action=register" class="simplemodal-register" >Register</a>
    <strong>or</strong>
<?php }
wp_loginout( get_permalink() ); ?>
</ul>

2 Answers 2

2

Try using wp_loginout(). It automatically handles those conditions.

6
  • Sorry Im somewhat of a noob. How can I implement this? Aug 3, 2012 at 6:54
  • put the above function wherever you want the login/logout link to appear thats it. This would show a logout link when the user is logged in and viceversa. Aug 3, 2012 at 6:57
  • I changed the code how you suggested, it's above. I made it so that it redirects to the current page but when I try to login from home it takes me to a post. Aug 3, 2012 at 7:17
  • paste your updated code Aug 3, 2012 at 7:30
  • Its the one in the post. That's how it looks now. See how I added <?php wp_loginout( get_permalink() ); ?> Aug 3, 2012 at 8:15
1

Try this:

<ul id="login-navigation" >
    <?php
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        echo __('Welcome') . ' <strong>' . $user->display_name . '!</strong> ';
    } else { ?>
        <a href="<?php echo site_url('wp-login.php?action=register') ?>" class="simplemodal-register" ><?php echo __('Register'); ?></a>
        <strong>or</strong>
    <?php } ?>
    <?php wp_loginout( $_SERVER['REQUEST_URI'] ); ?>
</ul>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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