Auto login, and redirect user after registering breaks email notifications.

Hello,

I am using a code snippet on one of my websites to auto login and redirect a new user after registering, but the redirect is preventing my email notifications from being sent to the new user, and the admin.

I am using a plugin for a front end registration form, and this is where the notifications are being set.

I have tried all variations that I can think of, and I have tracked the sticking point to be the wp_redirect().

Auto login works, and emails are sent when the homepage refreshes after registering.

The auto redirect works, as well, but no emails are sent. So, when a user registers, they are automatically logged in, and sent to the page of my choosing.

I feel that since the redirect is triggering before the rest of the page load happens, the email notifications are not being activated. The user registers, this triggers user_register() to run, they are auto logged in, and before the rest of the page can load, they are sent to a new page, stopping any additional functionality from completing.

Does this make sense? Can you think of any other possible scenarios, and/or solutions? I have been working on this for quite some time.

My code is:

// Auto Login User for CRED user registration form

function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);

// If a new user registered then redirect to our next registration step,
// otherwise, send user to the home page.

if ( isset($user_id) ) {
$url='/reg/';
} else {
$url= home_url();
}

wp_safe_redirect( $url );
exit;

}

add_action( 'user_register', 'auto_login_new_user' );

Thanks!