WordPress User Redirection / Auto Login After Registration
Table of Contents
In a previous post, I shared a code snippet that redirects users to a custom page immediately after they log in to their account in your WordPress powered website.
In today’s tutorial, I will be sharing the ProfilePress guide and code snippet that redirects users to a custom page on the successful completion of their account registration.
This could come in handy if you want to redirect users to say a thank you or congratulatory page after registration.
To automatically logged user in (auto-login) and then redirect them to a custom page after registration, go to ProfilePress settings, open the Registration section and then check Auto-login after registration
.
Having done this, users will now automatically be logged in after registration and then redirected to their WordPress dashboard by default.
To redirect them to a custom page or URL after the automatic login is done; go to Settings >> Redirection and setup the Login redirect.
Note: URL entered into the custom URL
field takes precedence over a selected page.
Via Shortcode
You can automatically log in users to their account and then redirect them to a custom page, using the redirect
attribute in ProfilePress registration shortcode.
For example, a registration page with the shortcode
[profilepress-registration id="1" redirect="https://xyz.com/welcome"]
will log users in and then redirect them to https://xyz.com/welcome
after they successfully register an account.
If you don’t want users to be logged in before the redirect happens, change “redirect” to “no-login-redirect” like so [profilepress-registration id="1" no-login-redirect="https://xyz.com/welcome"]
More info on the documentation page.
Using Code
Note that the codes shared below only work if the user registration is done in a custom front-end registration form. And the codes should go into your theme’s functions.php
file or your site-specific / dummy plugin.
To redirect users to say a thank you or welcome page after registration, the code below does exactly that.
add_action( 'ppress_after_registration', 'pp_redirect_after_registration' );
function pp_redirect_after_registration() {
$custom_page_url = 'https://xyz.com/thank-you/';
wp_redirect( $custom_page_url );
exit;
}
What if we want to redirect them based on the role they are assigned during registration? See the code below.
add_action( 'ppress_after_registration', 'pp_redirect_after_registration', 10, 3 );
function pp_redirect_after_registration( $form_id, $user_data, $user_id ) {
$a = get_user_by( 'id', $user_id );
//retrieve the user roles
$user_roles = $a->roles;
/**
* we'll redirect users with student role to https://xyz.com/welcome-student/
* and those with teacher role to https://xyz.com/welcome-teacher/
* and those with other roles to https://xyz.com/welcome/
*/
if ( in_array( 'student', $user_roles ) ) {
$redirect = 'https://xyz.com/welcome-student/';
}
elseif ( in_array( 'teacher', $user_roles ) ) {
$redirect = 'https://xyz.com/welcome-teacher/';
}
else {
// all other users
$redirect = 'https://xyz.com/welcome/';
}
wp_redirect( $redirect );
exit;
}
It is also worth noting that automatic login and redirection of users after registration can be done too via ProfilePress’ ppress_after_registration
action hook like so:
add_action( 'ppress_after_registration', 'pp_redirect_after_registration', 10, 3 );
function pp_redirect_after_registration( $form_id, $user_data, $user_id ) {
wp_set_auth_cookie( $user_id );
wp_set_current_user( $user_id );
$custom_page_url = 'https://xyz.com/welcome/';
wp_redirect( $custom_page_url );
exit;
}
If you have any pre-sale question, inquiring or contribution, get in touch.