How to Redirect to a Successful Registration Page in WordPress

Home » Snippets » How to Redirect to a Successful Registration Page in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Registrations are an integral part of any WordPress website that allows user interaction. However, not many webmasters know that they can actually control where users are redirected to after a successful registration. By default, WordPress keeps the users on the same page, which might not always be the ideal scenario. In this article, we will guide you on how to redefine your users’ journey by redirecting them to a custom page after successful registration.

					function wpturbo_redirect_after_registration($redirect_to, $requested_redirect_to, $user){
    if (!is_wp_error($user)) {
        $redirect_to = home_url();
    }
    return $redirect_to;
}
add_filter('registration_redirect', 'wpturbo_redirect_after_registration');
				

The code snippet essentially alters the default registration redirection mechanism of WordPress. When a user registers successfully, they are redirected to a specified URL instead of the default location.

Here’s a step-by-step explanation:

The first line of the code initiates a new function, called wpturbo_redirect_after_registration. This function will have the main role in changing the default post-registration page. It accepts three parameters: $redirect_to, $requested_redirect_to, and $user.

function wpturbo_redirect_after_registration($redirect_to, $requested_redirect_to, $user){

Let’s break these parameters:

  • $redirect_to is the URL to which WordPress will redirect the user after registration. By default, this is the dashboard.
  • $requested_redirect_to is a URL the user has requested to be redirected to, if any.
  • $user represents the user object. This carries user-specific data like username, email, etc.

Inside the function, we have an if statement that uses the is_wp_error($user) function to check if any registration errors have occurred. This function returns a Boolean value. If the parameter $user is an error, it will return true, otherwise, it will return ‘false’.

if (!is_wp_error($user)) {
        $redirect_to = home_url();
    }

When the registration is successful (meaning there’s no error, hence !is_wp_error($user) is true), we change the $redirect_to to the home URL of the site, by using the home_url() function which retrieves the home URL for the current site. So, a successful registration will now redirect the user to the home page.

The last part hooks our function into the WordPress workflow, in this case using the add_filter function. This function allows you to hook a custom function (in this case, wpturbo_redirect_after_registration) onto a specific filter action (registration_redirect) which fires after a user successfully registers. WordPress then uses the return value from the function in place of its default value.

add_filter('registration_redirect', 'wpturbo_redirect_after_registration');

So the final product is a successful registration on a WordPress site that redirects the user automatically to the homepage, not the dashboard or any other default location.

Register an account to save your snippets or go Pro to get more features.