SOLVED: Form custom redirect - Role-Based

I want to create a user role-based redirect using the bricks/auth/custom_login_redirect filter, but the documentation and the example provided in the following link are not clear enough for me: Bricks Builder Documentation.

I wrote the following code with the help of ChatGPT, which worked for redirection based on user roles, but it broke the login page:

add_filter('bricks/auth/custom_login_redirect', function($selected_login_page_id) {
    $user = wp_get_current_user();
    
    if (in_array('administrator', (array) $user->roles) || in_array('gna_admin', (array) $user->roles)) {
        return admin_url();
    } elseif (in_array('gna-member', (array) $user->roles)) {
        return home_url('/member-dashboard/');
    } else {
        return home_url(); 
    }
});

Could you please provide a clearer example or guidance on how to implement role-based redirects without breaking the login page?

Thank you for your support.

Hi @emmanuelson ,

This hook is for those who want to have a different login page based on PHP logic.

If I understand correctly, you want to redirect users to different pages after logging in, am I right?

If so, you can create a custom function and use {echo:} in Custom redirect URL (Form Element, Redirect action)
image

Regards,
Jenn

If i get you right, if i had the function below, i will echo it using the dd tag like so {echo:role_based_login_redirect} correct?

function role_based_redirection() {
    $user = wp_get_current_user();
    
    if ( in_array( 'administrator', $user->roles ) || in_array( 'gna_admin', $user->roles ) ) {
        wp_redirect( admin_url() );
        exit;
    } elseif ( in_array( 'gna-member', $user->roles ) ) {
        wp_redirect( home_url( '/member-dashboard/' ) );
        exit;
    }
}
add_action( 'wp_login', 'role_based_redirection' );

Hi @emmanuelson ,

Not using wp_login hook.

The role_based_redirection function just needs to return the URL strings without wp_redirect or exit function.

Example:

function role_based_redirection() {
    $user = wp_get_current_user();
	$redirect_url = 'https://mywebsite.com'; //default
    if ( in_array( 'administrator', $user->roles ) || in_array( 'gna_admin', $user->roles ) ) {
        $redirect_url = 'https://mywebsite.com/a';
    } elseif ( in_array( 'gna-member', $user->roles ) ) {
        $redirect_url = 'https://mywebsite.com/b';
    }
	
	return $redirect_url;
}

Then place {echo:role_based_redirection} in the form field.
Remember to whitelist role_based_redirection function.

Regards,
Jenn

Thank you, it worked.

HI itchycode

I adopted your code but it is still not working, the login is successful but stays on the login page without any redirect.
would you mind telling me what I am missing here?

thanks

function role_based_redirection() {
$user = wp_get_current_user();
$redirect_url = 'https://mywebsite; //default
if ( in_array( ā€˜administrator’, $user->roles ) || in_array( ā€˜business-editor’, $user->roles ) ) {
$redirect_url = ā€˜https:/mywebsite.com/’;
} elseif ( in_array( ā€˜editor’, $user->roles ) ) {
$redirect_url = ā€˜mywebsite.com is available for purchase - Sedo.com’;
}

return $redirect_url;

}

Hi @Anin-Al-Farah ,

Did you whitelist the function through bricks/code/echo_function_names hook?

Hi itchycode

Thanks a lot for coming back but unfortunately, it’s still not working, it does not matter who is logging in (admin or Editor) always goes to the default URL (Home Page).
Here is the Code I used:

function custom_login_redirect() {
$user = wp_get_current_user();
$redirect_url = ā€˜https://mysitetest.com’; //default
if ( in_array( ā€˜administrator’, $user->roles ) || in_array( ā€˜editor’, $user->roles ) ) {
$redirect_url = 'https://mysitetest.com/page01/ā€˜;
} elseif ( in_array( ā€˜editor’, $user->roles ) ) {
$redirect_url = 'https://mysitetest.com/page02/ā€˜;
}

return $redirect_url;

}

The custom page form has this function: {echo:custom_login_redirect}

Also, from Bricks Settings > Custom Code > Code Review - I copied the echo function and saved it on the snippet plugin.

Still no success…

Any Idea?

Many thanks

Hi @Anin-Al-Farah,

Please send your admin credentials to help@bricksbuilder.io and include this forum thread as a reference.
Let me know the page URL you are working on so I can directly check your issue.

Regards,
Jenn

Thanks, Itchycode

I was trying this on a local host before I implemented it on my live site, but I’ll try to duplicate it on the live site now and if it does not work I’ll create an admin account may be Bricks can help.
But in the meantime and if you do not mind me asking:
How can I use a plugin like ā€œLoginWP (Formerly Peter’s Login Redirect)ā€ with Bricks Custom Login Page instead of the Snippet Code? It has no function to add it to the Bricks form.

Kind Regards
Anin

Hi @Anin-Al-Farah ,

I am sorry that I never used that plugin before.
I just checked the plugin description, seems like you don’t need to use Bricks custom authentication pages if you are using that plugin.

Thanks a lot itchycode, i really appreciate your effort and help