WooCommerce Redirect Users After Registration by Roles

WooCommerce Redirect Users After Registration by WP User RolesRecently I undertook a WordPress development project that needed registration of custom user role using the default WooCommerce account. After review, the client made a request that we should separate the custom user roles redirection after the respective user registers on the site. So we needed WooCommerce redirect hook for every user based on their role.

Ideally, the first user would be the customer and the other user would be a member who did not need to make a payment before accessing restricted content.

To achieve this post registration redirect we need to accomplish a number of things, let me briefly outline how you can redirect users after registration based on their user role.

WooCommerce Redirect Based on User Roles

First, we need to check if the user role exists and if it does we can now use an if statement to conditionally redirect the different users to different pages. Let’s begin by checking for the existence of user roles.

// Check whether or not the current user specified roles

function check_user_role($roles, $user_id = NULL) {

    if ($user_id) $user = get_userdata($user_id);

       else $user = wp_get_current_user();

    if (empty($user)) return false;
       foreach ($user->roles as $role) {

    if (in_array($role, $roles)) {
      return true;
    }
 }
     return false;
}

After we establish that the user has the specified roles we now need to use the if statement that will conditionally test for the specific WordPress user role. If the role is found after registration, the WooCommerce registration redirect can be initiated for that specific role.

WooCommerce Redirect Users after Registration Example

Ideally, our aim is to redirect customers to a custom payment page after registration. The staff members can proceed to their account profile page after registration. This is how we achieve this WooCommerce redirect after registration in four steps.

  1. We create a function to redirect the specific user, Examplethe customer after registration.
  2. We call the function above check_user_role to ascertain the specific role we established in the step above.
  3. We return the URL where we want to redirect the customer inside the function we created.
  4.  Add WooCommerce registration redirect action.
function custom_registration_redirect() {

if (check_user_role(array('customer'))) {

        return site_url('/payment/');
     }

 }

add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);

Most new WordPress users or novice developer cannot figure out where to add this code, you can add this code to your functions.php file preferably in the child theme functions.php file.

Make sure you replace the respective section to match your user role and the redirect page. You can also extend the last if statement to add more redirects for more roles using the if-else statement of replacing it with a Switch statement altogether.

Finally, here is the complete code to redirect users after registration based on their roles in WooCommerce registration:

// Check whether or not the current user-specified roles

function check_user_role($roles, $user_id = NULL) {

  if ($user_id) $user = get_userdata($user_id);
        else $user = wp_get_current_user();

     if (empty($user)) return false;
   
    foreach ($user->roles as $role) {
     
  if (in_array($role, $roles)) {
      return true;
    }
   }
    return false;
}

// Create a Redirection Function + WP Action
   function custom_registration_redirect() {

      if (check_user_role(array('customer'))) { // Add the User Role in the Array

        return site_url(''); //Add your redirect page slug here
    }
}

add_action('woocommerce_registration_redirect', 'custom_registration_redirect', 2);

If you cannot still figure out how to get it done, please let me know how I can help you out in the comments section below.

 

Similar Articles

 

Comments are closed.