Support Forum
Support › MB User Profile › How to set auto login after a successfull registration page ?Resolved
Hello,
How can i allow auto-login after a user fill a registration form and submit ?
I use this in my registration page :
[mb_user_profile_register id='user-register-extra' password_strength='weak' email_as_username='true' label_password='Password (min. 8 alphanumeric)' redirect='https://fic-indonesia.com/kelas/' confirmation='Akun anda sudah berhasil dibuat.']
but it is redirecting to the page https://fic-indonesia.com/kelas/ in a LOGGED-OUT state
Thank you
Joie
Hi,
You can follow this topic to log in user with coding after registering successfully
https://wordpress.stackexchange.com/questions/337998/how-to-auto-login-after-registration
Hi Long
Similar problem here
I followed your link and the autologin works well
But the extra fields I added in the registration form (custom and regular) are not saved anymore
Well, somehow it saves only the user_url
Before adding the autologin function data were all saved as aspected
It seems I'm using the wrong WP hook and the autolign fires before META plugin complete it's work
Any clues?
Here's my code:
add_filter( 'rwmb_meta_boxes', 'agri_new' );
function agri_new( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Submitter', 'agri' ),
'id' => 'user-extra',
'type' => 'user',
'fields' => [
[
'name' => __( 'Nome', 'agri' ),
'id' => $prefix . 'first_name',
'type' => 'text',
'required' => true,
],
[
'name' => __( 'Cognome', 'agri' ),
'id' => $prefix . 'last_name',
'type' => 'text',
'required' => true,
],
[
'name' => __( 'Telefono', 'agri' ),
'id' => $prefix . 'user_phone',
'type' => 'text',
'required' => true,
],
[
'name' => __( 'Sito web', 'agri' ),
'id' => $prefix . 'user_url',
'type' => 'url',
],
],
];
return $meta_boxes;
}
Here's the custom function:
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
if(awr_user_has_role(get_current_user_id(), 'subscriber')){
wp_redirect("/proponi-un-luogo/");
}
exit();
}
add_action( 'user_register', 'auto_login_new_user' );
Hi Andrea,
You can change the action hook user_register
to rwmb_profile_after_save_user
to save data before redirecting to a page.
add_action( 'rwmb_profile_after_save_user', 'auto_login_new_user' );
Refer to the documentation https://docs.metabox.io/extensions/mb-user-profile/#user-actions
Grazie mille! 🙂
Long you’re an angel… and I’m quite ashamed of my laziness… I should definitely pay more attention to the documentation. There’s already everything in it. Indeed is probably the best api documentation I ever used: clear, accurate, complete, in depth, useful. I love your great work every day more.
Here again just to post the final code to whom may be interested
Obviously I had to check the doc to get it working because user_register pass a number while rwmb_profile_after_save_user pass an object
function auto_login_new_user( $user ) {
$id = $user->user_id;
wp_set_current_user($id);
wp_set_auth_cookie($id);
if(awr_user_has_role($id, 'subscriber')){
wp_redirect("/proponi-un-luogo/");
}
exit();
}
add_action( 'rwmb_profile_after_save_user', 'auto_login_new_user' );
Andrea
What is awr_user_has_role function ?
Hi Joie
Just a custom function to check user role
function awr_user_has_role ($user_id, $role) {
if (is_numeric( $user_id))
$user = get_user_by( 'id', $user_id );
else
$user = wp_get_current_user();
if (empty($user))
return false;
return in_array($role, (array) $user->roles);
}
Thank you Andrea.
Will try that!.
Is that action hook also called when logged in user updating their profile ?