Mar*_*rio 4 passwords wordpress login autologin
我正在尝试在注册后自动登录用户。
我正在尝试这个(functions.php):
add_action( 'user_register', 'auto_login_user' );
function auto_login_user($user_id) {
$user = new WP_User($user_id);
$user_login_var = $user->user_login;
$user_email_var = stripslashes($user->user_email);
$user_pass_var = $user->user_pass;
$creds = array();
$creds['user_login'] = $user_login_var;
$creds['user_password'] = $user_pass_var;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
echo $user->get_error_message();
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:您为用户名“TheNewUserCreated”输入的密码不正确。忘记密码?
如何从 User 对象中获取密码?
也因为这是模板 registration.php 中的自定义注册过程,我尝试将其与 $_POST 一起使用并在该文件中运行该函数,但我也没有成功...
编辑: 好的,我得到了加密密码,那么这里的解决方案是什么,我如何自动登录用户?也许我可以在registration.php 页面中做到这一点?
小智 5
将以下函数添加到functions.php文件中
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
// You can change home_url() to the specific URL,such as
//wp_redirect( 'http://www.wpcoke.com' );
wp_redirect( home_url() );
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
Run Code Online (Sandbox Code Playgroud)
如果您使用wp_insert_user();注册用户然后自动登录他们,这很简单。如果成功,该函数将返回用户 ID,因此可以使用它来登录该用户。
$id = wp_insert_user($data);
//so if the return is not an wp error object then continue with login
if(!is_wp_error($id)){
wp_set_current_user($id); // set the current wp user
wp_set_auth_cookie($id); // start the cookie for the current registered user
}
Run Code Online (Sandbox Code Playgroud)
但要遵循你已经拥有的,它可以是这样的:
add_action( 'user_register', 'auto_login_user' );
function auto_login_user($user_id) {
wp_set_current_user($user_id); // set the current wp user
wp_set_auth_cookie($user_id); // start the cookie for the current registered user
}
//this code is a bit tricky, if you are admin and you want to create a user then your admin session will be replaced with the new user you created :)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9617 次 |
| 最近记录: |