WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Log In, request for reauth on wp-admin (6 posts)

  1. Charles Leonard
    Member
    Posted 2 years ago #

    Greetings,

    I have created a custom log in page with the wp_signon function and when I link the administrative users to the dashboard via wp-admin, they're requested to log in again. Can anyone fill me in on how to authenticate an administrative user for logging into the dashboard from my custom log in page?

    Thanks,
    Charles Leonard

  2. Charles Leonard
    Member
    Posted 2 years ago #

    Apparently the wp_set_auth_cookie() function is all that's required. I placed this into my log in script, and it's now seamless between log in and admin dashboard.

    wp_set_auth_cookie($user_data->ID);

  3. RobinWellner
    Member
    Posted 2 years ago #

    Hi,

    We are having the same problem you had. Unfortunately, from your answer I still don't know how to fix it. Where exactly did you place the line wp_set_auth_cookie($user_data->ID);?

  4. Charles Leonard
    Member
    Posted 2 years ago #

    Robin,

    I'm storing get_user_by() data in the $user_data variable, and after the wp_signon() is verified is when I call wp_set_auth_cookie($user_data->ID);. The ID is part of the get_user_by() array.

    Hope this helps, if so let me know.

    Charles

  5. getrouty
    Member
    Posted 1 year ago #

    You sir, are a gentlemen and a scholar. Thanks! =) Just for kicks, here is a working example after receiving the post from the login form:

    if ($_POST) {
    
        $errors = array();
    
        $username = $wpdb->escape($_REQUEST['username']);
        $password = $wpdb->escape($_REQUEST['password']);
        $remember = $wpdb->escape($_REQUEST['rememberme']);
        $remember = ($remember) ? "true" : "false";
    
        $login_data = array();
        $login_data['user_login'] = $username;
        $login_data['user_password'] = $password;
        $login_data['remember'] = $remember;
        $user_verify = wp_signon($login_data, true);
    
        if (is_wp_error($user_verify)) {
            $errors[] = 'Invalid username or password. Please try again!';
        } else {
            wp_set_auth_cookie($user_verify->ID);
    
            if(isset($_POST['redirect_to'])){
                $go_to = $_POST['redirect_to'];
            } else {
                $go_to = home_url();
            }
    
            wp_redirect($go_to);
            exit;
        }
    
    }
  6. sohail_mubeen
    Member
    Posted 1 year ago #

    thanks getrouty :)

Topic Closed

This topic has been closed to new replies.

About this Topic