WordPress User Redirection / Auto Login After Registration
Do you want to redirect new users to a custom page immediately after they sign up and auto-log them into your WordPress website?
In a previous post, we shared a code snippet that redirects users to a custom page immediately after they log in to their account on your WordPress-powered website.
Building on that, in this tutorial, I’ll share the ProfilePress guide and a code snippet that redirects users to a custom page upon completing their account registration.
This feature could be handy if you want to redirect users to a thank-you or congratulatory page after registration.
Setting Up Redirect & Auto Login After WordPress Registration
We will begin by purchasing and installing the ProfilePress plugin. To do this, Visit the ProfilePress pricing page and choose the pricing plan that best suits your needs.
After purchasing, download the plugin to your computer. Next, navigate to your WordPress admin dashboard, proceed to the Plugins page, and click the “Add New” button.
Then select “Upload Plugin” and click “Choose file” to locate the previously downloaded plugin zip file. Finally, click “Install Now” to complete the installation process.
After activating and configuring the plugin on your website, proceed to enable auto-login and set up redirection for users after registration.
To auto-login users after registration, navigate to ProfilePress settings, access the Registration section, and then check the Auto-login after registration checkbox setting.
After doing this, users will automatically be logged in after registration and redirected to their WordPress dashboard by default.
To redirect them to a custom page or URL after the automatic login, go to Settings >> Redirection and set up the Login redirect.
Note: The URL entered into the custom URL
field takes precedence over a selected page.
User Redirection & Auto-Login After Registration Via Shortcode
You can automatically log in users to their accounts 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.
Setting Up User Redirection/ Auto Login After Registration Using Code
Note that the codes shared below only work if the user registration is done in a custom front-end registration form. The codes should go into your theme’s functions.php file or your site-specific / dummy plugin.
The code below redirects users to say a thank you or welcome page after registration.
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 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;
}
Set Up Your Auto-Login & Redirection After WordPress Registration Today!
In conclusion, customizing your WordPress registration flow by redirecting new users to a custom page upon sign-up while automatically logging them into your website can significantly enhance user experience and engagement.
With the guidance provided in this tutorial, you can implement these features using the ProfilePress plugin and/or code snippets.
Remember, a well-designed registration flow is the foundation of a successful website, and it can make all the difference in attracting and retaining users. So, take the time to set up auto-login and redirection after registration for your WordPress website today, and see the difference it can make in your user engagement and conversions.
If you have any pre-sale questions, inquiries, or contributions, get in touch.