Skip Navigation

[Resolved] Correct redirect doesn't work after auto login using cred user creation

This support ticket is created 2 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 3 voices.

Last updated by simonM-5 2 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2354629

Dear Support

I followed the guide: https://toolset.com/forums/topic/automatically-log-in-after-user-is-created/ to create a new Custom Code Snippet "auto-login-after-cred-user-creation". The auto login appears to work correctly but the redirect to the correct page is not.

Quick background:
We have two groups of users, Nannies and Families.

Before the snippet, the users created themselves via a Toolset User Form, and depending on which link they chose, a different role is assigned.

User clicks Sign Up: User Form - New Family (1308) is activated and user is assigned role of Unverified Family.
User clicks Become a Native Nanny: User Form - New Native Nanny (1242) is activated and user is assigned role of Unverified Nanny.

After the completion of the simple user form (First Name, Last Name, Email and Password), the users are required to log in so that they can fill out their details and hence create a post in either the Post Type Families or the Post Type Nannies respectively.

If a user breaks off the sign-in process at any time and has to come back to it at any point, they just log in again, and they are redirected correctly to the appropriate page. Only when the login is automatic they are not redirected to the correct page.

I suspect this could be a conflict somehow with the existing Custom Code Snippet "conditional-redirect-on-successful-login" and think perhaps some tiny tweak in the code might be necessary. Could it be something to do with the order in which the code snippets are being executed maybe?

Feel free to reproduce the 404 error by clicking Sign Up or Become a Native Nanny links in the menu.

We'd appreciate your expert assistance as always to get this working smoothly as it would be a HUGE boost in streamlining our sign-up process.

Thanks and kind regards
Simon

#2355187

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Simon

The existing login redirect code you have is hooked in using the "login_redirect" filter (https://developer.wordpress.org/reference/hooks/login_redirect/). That is a WordPress filter that is triggered during the normal login process.

The code you have to automatically login users that complete the registration form uses the wp_signon function. That is not part of the normal login process, and the login_redirect filter never gets triggered, which is why that code doesn't run.

I think you should just be able to set up the redirection you want in the form settings (the redirection happens after the form submission has completed, i.e. after the auto login code you trigger using the cred_save_data hook).

It looks at the moment like you redirect users to the login page, but if they are already logged in (because you auto log them in) that's not where they should be redirected to.

Try changing the form settings to where you want the logged-in new users to go.

#2355213

Hi Nigel

I changed the User Form - New Family User (1308) to go to page "Sign Up - Complete Profile" instead. But now the autologin doesn't work and I get a 404.

Kind regards
Simon

#2355335

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've disabled the hook "cred_save_data" you added to your snippet "auto-login-after-cred-user-creation":

//add_action( 'cred_save_data', 'new_cred_user_autologin', 10, 2 );

And I've added the following hook to the same snippet "auto-login-after-cred-user-creation":
=> hidden link

add_action('cred_submit_complete', 'func_auto_login_after_register',10,2);
function func_auto_login_after_register($post_id, $form_data) {
    // User Form - New Family 1308
	// User Form - New Native Nanny 1242
	$forms = array( 1308, 1242);
	if ( in_array($form_data['id'],$forms) ) { // Edit as required
      
      	$user_id = $post_id;
		if ( !empty( $_REQUEST['user_email'] ) && !empty( $_REQUEST['user_pass'] ) ) {

			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);
            }

		}
	}
}

Could you please confirm it works as expected now. I checked and after registration it successfully redirect me to profile update page.

#2355411

Hi Minesh/Nigel

Thank you once again for the fantastic fast support! 🙂 We have tested in our dev and www sites and all is working well!

Thanks and regards
Simon

#2355413

My issue is resolved now. Thank you!