Wordpress- hook to auto login after registration

Question: How can an user auto logged in after registration in wordPress? 
Answer: Using below hook/action/code, user will auto login after registration in wordPress.

 
function _new_user_auto_log_in($user_id){
  if(!is_user_logged_in()){
    $secure_cookie = is_ssl();
    $secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
    global $auth_secure_cookie;
    $auth_secure_cookie = $secure_cookie;
   
    wp_set_auth_cookie($user_id, true, $secure_cookie);
    $user_info = get_userdata($user_id);
    do_action('wp_login', $user_info->user_login, $user_info);

  }
}
add_action('user_register', '_new_user_auto_log_in');

No comments:

Post a Comment