I've been using this code in the functions.php file to redirect users to different pages based on role. It recently stopped working. Has there been a change to Toolset that requires an update to this code?
//redirect students to student page
function my_login_redirect( $redirect_to, $request, $user ) {
if( !isset( $user->user_login ) ){ // only run when credentials have been supplied
return $redirect_to;
}
//is there a user to check?
if (isset($user->roles) && is_array($user->roles)) {
//check for students
if (in_array('students', $user->roles)) {
// redirect to student collab page
$redirect_to = 'hidden link';
}
else{
$redirect_to = 'hidden link';
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Hello and thank you for contacting Toolset support.
Maybe there is a hook that runs after this one and changes the redirect URL again? What would you get if you try a higher priority:
add_filter( 'login_redirect', 'my_login_redirect', 10000, 3 );
If it does not help, activate PHP debugging then add calls to error_log to check if the hook is being called or not. Something like:
function my_login_redirect( $redirect_to, $request, $user ) {
error_log('hook is running');
if( !isset( $user->user_login ) ){ // only run when credentials have been supplied
return $redirect_to;
}
//is there a user to check?
if (isset($user->roles) && is_array($user->roles)) {
//check for students
if (in_array('students', $user->roles)) {
// redirect to student collab page
$redirect_to = '<em><u>hidden link</u></em>';
}
else{
$redirect_to = '<em><u>hidden link</u></em>';
}
}
error_log( $redirect_to );
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
If the hook runs correctly, you should get the logs in the debug.log file.
Check this article https://wordpress.org/support/article/debugging-in-wordpress/
If that does not help, I'll need access to WordPress and FTP to check this closely. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
I will need to check in a minimal setup(Default theme+only Toolset plugins). Can I do it on the live site? Or can I take a copy of your website and debug it locally?
Please make a copy and debug locally.
Thank you! However, I am not able to install a plugin in order to take the copy. This is what I get when I try to install the Duplicator plugin hidden link
Can you install the Duplicator plugin and prepare a copy? Otherwise, can you update your previous private reply with FTP credentials so I can do it? Your next reply will also be private to let you share credentials safely.
The plugin has been installed an a package created for you.
I took the copy and build it locally. I could not work on it because of an error from the AuthLDAP plugin, so I deactivated it.
Because the hook is a default WordPress hook, I started by deactivating all the plugins, Toolset plugins included. Then, I tried to log in with a student account, and the custom code worked as expected, and I was redirected to the correct page. I made a small update to the custom code to use WP_HOME instead of the hardcoded domain:
function my_login_redirect( $redirect_to, $request, $user ) {
if( !isset( $user->user_login ) ){ // only run when credentials have been supplied
return $redirect_to;
}
//is there a user to check?
if (isset($user->roles) && is_array($user->roles)) {
//check for students
if (in_array('students', $user->roles)) {
// redirect to student collab page
$redirect_to = WP_HOME . '/msan-intersectional-social-justice-collaborative/';
}
else{
$redirect_to = WP_HOME;
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10000, 3 );
Then, I activated Toolset plugins, and I tried from the wp-login.php page, and from the Toolset form on the homepage, and it worked as expected. I activated all the other plugins, except AuthLDAP, and the custom code still worked as expected.
I suspect that the AuthLDAP plugin is also hooking into the "login_redirect" filter. Please, check on your website while deactivating this plugin.
If that does not help, please deactivate all Toolset plugins and try from wp-login.php, if it worked on it, it must also work with Toolset forms.
Let us know what you will get.