Soporte » WordPress Avanzado » Login without password using wp_set_auth_cookie()

  • Daniel P.


    Hello, I’m trying to login some users using a function found in docs.

    The function does the login and all works great on the frontend, the problem is in the backend, when trying to access it asks for username and password (wp-login.php)

    Is like when trying to access the backend the cookie is lost and WP asks to login again.

    Function I’m using:

    function login_external_user($username) {
        $user = get_user_by('login', $username );
        // If no error received, set the WP Cookie
        if ( !is_wp_error( $user ) )
            wp_set_current_user ( $user->ID ); // Set the current user detail
            wp_set_auth_cookie  ( $user->ID, true ); // Set auth details in cookie
            $message = "Logged in successfully";
        } else {
            $message = "Failed to log in";
        echo $message;

    I need to say also that this code runs on a custom endpoint as it receives data from another website. When data is received on

    add_action( 'init', function(){
        add_rewrite_endpoint( 'login_sso', EP_ROOT );
    } );
    add_action( 'template_redirect', function() {
        if ( $login_sso_Url = get_query_var( 'login_sso' ) ) {
            //do stuff
            // then stop processing

    Any ideas?

Viendo 1 respuesta (de un total de 1)
  • Iniciador del debate Daniel P.


    I have found that when using wp_set_auth_cookie inside a function in functions.php does set the cookies but not the wordpress_sec_ cookie correctly… So when entering on the backend the system fails and need to login again.

    Any ideas?

Viendo 1 respuesta (de un total de 1)
  • El debate ‘Login without password using wp_set_auth_cookie()’ está cerrado a nuevas respuestas.