Support » Fixing WordPress » How to auto login a user

  • I have the following function for auto logging in a user. But every time I try it I get the wordpress login form.

    I can see that the cookie gets set. Is there anything I am missing?

    function auto_login( $user_login = null ) {
        if( !is_user_logged_in() ) {
            // get user's ID
            $user = get_user_by( 'login', $user_login );
            $user_id = $user->ID;
    
            // auto login to WordPress
            wp_set_current_user( $user_id, $user_login );
            wp_set_auth_cookie( $user_id );
            do_action( 'wp_login', $user_login, $user );
    
            // auto login to WordPress
            wp_redirect( $_GET['redirect_to'] );
            exit;
        }
    
    } // end auto_login()
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter frank tredici

    (@frank13)

    Note:

    When I first login to a site in my Multisitre network, the following cookies get set:
    wordpress_d6718066b5ad6e44d3470f952222ec16
    wordpress_logged_in_d6718066b5ad6e44d3470f952222ec16
    wordpress_test_cookie
    wp-settings-1
    wp-settings-time-1

    And I am taking to my Admin panel.

    Yet when I switch to another site in the network, I only get the following cookie and the WP login form appears and I cannot get past it.
    wordpress_test_cookie

    I do not get the other 4 cookies.

    Thread Starter frank tredici

    (@frank13)

    I am wondering if it has anything to do with the cookie attribute isHttpOnly?

    I can see that the cookie gets set on wp_set_auth_cookie(); do_action(); with attribute isHttpOnly = “true”, yet once the wp_redirect(); is executed (which is essentially back to itself) the cookie is destroyed.

    BTW, my script was working fine until I upgrade yesterday to WP v4.7.

    Thread Starter frank tredici

    (@frank13)

    Any wordpress gurus out there?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to auto login a user’ is closed to new replies.