How to set auto login after a successfull registration page ?
Support › MB User Profile › How to set auto login after a successfull registration page ?Resolved
- This topic has 8 replies, 3 voices, and was last updated 3 years ago by
[email protected].
-
AuthorPosts
-
April 6, 2022 at 1:56 PM #35487
[email protected]
ParticipantHello,
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
JoieApril 7, 2022 at 12:31 PM #35503Long Nguyen
ModeratorHi,
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-registrationApril 7, 2022 at 8:23 PM #35522Andrea Guerriero
ParticipantHi 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' );
April 8, 2022 at 12:37 PM #35531Long Nguyen
ModeratorHi Andrea,
You can change the action hook
user_register
torwmb_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
April 8, 2022 at 2:05 PM #35535Andrea Guerriero
ParticipantGrazie 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.April 8, 2022 at 4:20 PM #35538Andrea Guerriero
ParticipantHere 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 objectfunction 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' );
April 14, 2022 at 5:27 PM #35634[email protected]
ParticipantAndrea
What is awr_user_has_role function ?
April 14, 2022 at 6:14 PM #35635Andrea Guerriero
ParticipantHi Joie
Just a custom function to check user rolefunction 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); }
April 14, 2022 at 6:18 PM #35636[email protected]
ParticipantThank you Andrea.
Will try that!.
Is that action hook also called when logged in user updating their profile ?
-
AuthorPosts
- You must be logged in to reply to this topic.