Bricks Form -- redirect after login based on user roles

I have a working function for Bricks Builder forms that will redirect based on user roles. This version is adapted to work on a site that I developed which has multiple login forms, so you will notice that:

  • $validFormIds accepts an array
  • there is an “if ($formId ==” section to work out the email field id depending on which form

I have adapted it to recognise whether the login is an email or username using FILTER_VALIDATE_EMAIL

** Edited with improved php logic – Special thanks to @MartinWB for excellent suggestions

<?php 

function bricks_login_form_redirect_action( $form ) {  
    $fields = $form->get_fields();
    $formId = $fields['formId'];
    // $settings = $form->get_settings();

    // Perform some logic here...

    $validFormIds = array('lnuzdj', 'hperbv'); // Add all the valid form IDs to this array

    if (in_array( $formId, $validFormIds)) {

        // error_log('Form ID: ' . $formId);

        // get the email address from that field on the login form
        if ($formId == 'lnuzdj') {
            $login = $fields['form-field-8bbefb'];
        } elseif ($formId == 'hperbv') {
            $login = $fields['form-field-373317'];
        } else {
            $login = ''; // Default value if the form ID doesn't match any known form
        }

        if( $login ) {

            
            // Is the field an email or username? Validate and then get user data
            if (filter_var($login, FILTER_VALIDATE_EMAIL)) {
                $user = get_user_by('email', $login);
            } else {
                $user = get_user_by('login', $login);
            }

            // get user roles
            $roles = ( array ) $user->roles;

            // error_log('User ID: ' . $user_id);
            // error_log('User Roles: ' . implode(', ', $roles));

            $base_url = get_home_url();

            $role_redirects = [
                'administrator' => '/wp-admin/',
                'patient' => '/patient-dashboard/',
                'clinic_admin' => '/clinic-admin/',
                'customer' => '/shop/',
                'subscriber' => '/shop/',
                'shop_manager' => '/wp-admin/',
            ];

            $default_redirect = '/shop/';

            $redirect_to = $base_url . $default_redirect; // Default redirection

            foreach ($role_redirects as $role => $path) {
                if (in_array($role, $roles)) {
                    $redirect_to = $base_url . $path;
                    break;
                }
            }

            // error_log('Redirecting to: ' . $redirect_to);

            $form->set_result([
                // 'action' => 'my_custom_redirect',
                'type'    => 'redirect',
                'redirectTo' => $redirect_to,
                'redirectTimeout' => 0
            ]);

        }

    }
}
add_action( 'bricks/form/custom_action', 'bricks_login_form_redirect_action', 10, 1 );

Form Action Settings:
image

If anyone has improvements let me know

2 Likes

Nice one. I would suggest to delete redirect action as it suppose to run from your custom action. Also I would make base url as variable so you may change it quickly if needed instead of rewriting more urls.

This may be easier to maintain in future

$base_url = 'https://www.example.com/';
$role_redirects = [
    'administrator' => 'wp-admin/',
    'patient' => 'patient-dashboard/',
    'clinic_admin' => 'clinic-admin/',
    'customer' => 'shop/',
    'subscriber' => 'shop/',
    'shop_manager' => 'wp-admin/',
];

$default_redirect = 'shop/';

$redirect_to = $base_url . $default_redirect; // Default redirection

foreach ($role_redirects as $role => $path) {
    if (in_array($role, $roles)) {
        $redirect_to = $base_url . $path;
        break;
    }
}

2 Likes

Hi
Can you explain in clear detail how you get it working? i managed to get it working with the following code, the login process is successful but no redirect at all, it stays on the login page.
Any help on this will be greatly appreciated.
Here is the code:

function custom_role_based_redirect($user_login, $user) {
$base_url = ‘https://mysite.com/’;

// Define role-based redirects
$role_redirects = [
    'administrator' => 'wp-admin/',  // Redirect administrators to the dashboard
    'editor' => 'custom-page/',  // Adjust this path as needed
];

$default_redirect = 'wp-admin/';  // Default redirection

// Initialize the redirect URL to the default
$redirect_to = $base_url . $default_redirect; 

// Check if the user has any of the specified roles
foreach ($role_redirects as $role => $path) {
    if (in_array($role, $user->roles)) {
        $redirect_to = $base_url . $path;
        break;
    }
}

// Perform the redirect
wp_redirect($redirect_to);
exit;

}

// Hook the function to the ‘wp_login’ action
add_action(‘wp_login’, ‘custom_role_based_redirect’, 10, 2);

Make sure that you are using the full code from the top of this thread (modified to suit your exact form and field IDs): Bricks Form -- redirect after login based on user roles

From you snippet it looks like you’re missing a bunch of the setup stuff. Maybe you copied from Martin’s follow-up code which does not include the form setup as shown here:

It should work for you as long as you use that code and make sure to use your own form and field IDs.