Front end login and check not working correctly

I’m using the code below to allow front-end login, I then have a different menu depending if they’re logged in or not.

The problem I’m having is that once the user clicks login and they get redirected to another page, the menu that is shown there is correct but when they click on another page it reverts to the “not logged in” menu even though no logout buttons have been clicked.

Read More
<form name="loginform" id="loginform" action="<?php echo esc_url( site_url( 'wp-login.php', 'login_post' ) ); ?>" method="post">
  <p>
    <label for="user_login">
      <?php _e('Username') ?>
      <br />
      <input type="text" name="log" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="20" tabindex="10" />
    </label>
  </p>
  <p>
    <label for="user_pass">
      <?php _e('Password') ?>
      <br />
      <input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" />
    </label>
  </p>
  <?php do_action('login_form'); ?>
  <p class="forgetmenot">
    <label for="rememberme">
      <input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90"<?php checked( $rememberme ); ?> />
      <?php esc_attr_e('Remember Me'); ?>
    </label>
  </p>
  <p class="submit">
    <input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="<?php esc_attr_e('Log In'); ?>" tabindex="100" />
    <?php   if ( $interim_login ) { ?>
    <input type="hidden" name="interim-login" value="1" />
    <?php   } else { ?>
    <input type="hidden" name="redirect_to" value="<?php bloginfo('url'); ?>/dashboard" />
    <?php   } ?>
    <input type="hidden" name="testcookie" value="1" />
  </p>
</form>

The code below is what I’m using around the menu to show a different one depending if they’re logged in or out:

<?php if ( is_user_logged_in() ) {
} else {
} ?>

It looks as though the session is getting destroyed once logged in. It saves it initially but clicking on any other links destroys the session!

Related posts

Leave a Reply

2 comments

  1. Try it slightly differently, as below:

     <?php 
    
      if(!is_user_logged_in()) {
             //no user logged in
          } else {
            // Hello!
          }
     ?>
    

    Edit: It appars that is_user_logged_in() has some issues: http://core.trac.wordpress.org/ticket/21043

    We can do this in a diffreent approach, of course. Let’s get the user information, and check variables based on that.

    <?php
        global $current_user; // Make it global
        get_currentuserinfo(); // Get the current user info
        $userID = $current_user->ID; // Get the User ID
    
        if($userID) { // Logged in because there is a User ID. 
    
            echo "Hello Mate";
    
        } else { // Logged out
    
            echo "Tumbleweed";
        } 
    ?>
    

    I was scratching my head when this didnt work in my environment, so make sure you aren’t logged in as admin in the same browser.

    You are also welcome to put this into a function, depending on what your after.

  2. Try installing the “theme my login” plugin, besides making your login page as a front-end page, it will add a widget with a quick login form.