is_user_logged_in() returns false after wp_signon

1.6k Views Asked by At

I am using custom login form and here is the code. After successfull login it redirects to /portal page but again in portal page user logged in status is false.

$email = $_REQUEST['login_email'];
        $password = $_REQUEST['login_password'];

        $args = array(
            'orderby'      => 'login',
            'order'        => 'ASC',
            'fields'       => 'all'
            );
        $count=0;
        $all_users = get_users( $args );
        $login_data['user_password'] = $password;
        $login_data['user_login'] = $email;
        $login_data['remember'] = true;
        $user_verify = wp_signon( $login_data, true );

        $userID = $user_verify->ID;

        do_action( 'wp_login', $email );
        wp_set_current_user( $userID, $email );
        wp_set_auth_cookie( $userID, true, false );


        if ( is_wp_error($user_verify) ){
            $loginerror = true;
            $errors['login_error']=__("Error in login",'domain');
        }
        else{
            $loginSuccess = true;
            $_SESSION['login_email']=$email; // Initializing Session
            if(empty($previous_location))
            {

                wp_safe_redirect( home_url('/portal/'));
            }
            else{
                wp_safe_redirect($previous_location);
            }
            exit();
        }

But in /portal page when i var_dump(is_user_logged_in()) it returns false. what is happening here? what have i done?

2

There are 2 best solutions below

0
Rafał R On

I had the same experience. It turned out that my custom registration mechanism was the fault. I was adding new users with custom sql queries using $wpdb object and insert method.

Somehow this approach conflicts with latter wp_signon usage. If you also have written custom registration mechanism try falling back to wp_insert_user method and then update custom columns or whatever with $wpdb->update.

0
Kurisubo On

I know this question is old, but I struggled for so long with this issue, I just want to share my knowledge for the next person.

My problem was that on page reload the page headers were already sent and therefore causing problems for wp_signon() to work properly. To fix this I created this function and put it in my functions.php file:

add_action('template_redirect', 'my_check_login');
function my_check_login(){
    if( is_page_template('custom-login.php') ){
        global $wpdb; 

        //SQL Escape all inputs
        $email = $wpdb->escape($_POST['email']);  
        $password = $wpdb->escape($_POST['password']);  
        $remember = $wpdb->escape($_POST['rememberme']);  

        if($remember) $remember = "true";  
        else $remember = "false";

        $login_data = array();  
        $login_data['user_login'] = $email;  
        $login_data['user_password'] = $password;  
        $login_data['remember'] = $remember;  

        $user_verify = wp_signon( $login_data, false );
        if ( is_wp_error($user_verify) ) {  
            echo "Invalid login details";  
        } else {    
            wp_set_current_user($user_verify->ID);
            wp_set_auth_cookie($user_verify->ID,true);
            echo "<script type='text/javascript'>window.location.href='". home_url() ."'</script>";
            exit();  
        }
    } else {  
        // Add user feedback here
        //echo "Invalid login details";  
    }  
}

Where my custom-login.php file is just a simple form:

<?php  
/** 
 * Template Name: Login page 
 */  
 get_header();   

?>  
<div class="background-light">
    <div class="container">
        <div class="row">
            <div class="offset-3 col-6">
                <form id="login1" class="registration-form" name="form" action="<?php echo home_url(); ?>/login/" method="post">
                    <input id="email" type="text" placeholder="Email" name="email"><br>  
                    <input id="password" type="password" placeholder="Password" name="password">  
                    <input id="submit" type="submit" name="submit" value="Submit">  
                </form>
            </div>
        </div>
    </div>
</div>