简体   繁体   中英

Wordpress login via script

I have tried to put together a php page that will log a user in/or create them and log them in. My user is getting created but when i try to go to a page WP just re-directs me to the actual login page. Below is the part that i cant get to work.

if ( !username_exists( $user_login_name ) ) {
    $user_id = wp_create_user( $user_login_name, md5($user_login_name), "" );
    $creds = array();
    $creds['user_login'] = $user_login_name;
    $creds['user_password'] = md5($user_login_name);
    $user = wp_signon( $creds, false );
    wp_set_current_user($user_id);
    if ( is_wp_error($user) )   {
        echo $user->get_error_message();
    }
    wp_set_auth_cookie( $user_id );
} else  {
    $user = get_user_by('login', $user_login_name);
    $user_id = $user->ID;
    $creds = array();
    $creds['user_login'] = $user_login_name;
    $creds['user_password'] = md5($user_login_name);
    $user = wp_signon( $creds, false );
    wp_set_current_user($user_id);
    if ( is_wp_error($user) ){
        echo $user->get_error_message();            
    }
    wp_set_auth_cookie( $user_id );

}

Can anyone tell me what im doing wrong? Ive set the cookie, the current user, etc. But Still a no go.

Add 'remember' to the $creds array:

$creds['remember'] = true;

That ought to keep you logged in until the natural WP session expires.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM