Problem: I am displaying a login form on my site using the Views login form shortcode wpv-login-form. The site is in 3 languages, so the shortcode has been translated. The redirect URL is different depending on the language. That's fine, but I would like to redirect certain User roles to a different URL. Again, the URL is different for each language.
Solution: Keep the login shortcodes as they are currently written and translated. Use the default redirect URLs in the shortcodes. Use the following custom code to redirect specific User roles to another URL:
function my_login_redirect( $redirect_to, $request, $user ) { //is there a user to check? if ( isset( $user->roles ) && is_array( $user->roles ) ) { //check for admins if ( in_array( 'administrator', $user->roles ) ) { // redirect them to the default place return $redirect_to; } elseif ( in_array( 'family', $user->roles ) ) { return $redirect_to; }elseif( in_array( 'native_nanny', $user->roles ) ){ switch( $redirect_to ) { case get_site_url() . '/de/native-nanny-finden': $redirect_to = get_site_url() . '/de/job-finden'; break; case get_site_url() . '/es/encuentra-tu-native-nanny': $redirect_to = get_site_url() . '/es/encuentra-un-trabajo'; break; default: $redirect_to = get_site_url() . '/en/find-a-job'; break; } } } return $redirect_to; } add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Note that these redirects are very tightly coupled to the URLs used in the login form shortcode and the URLs of the actual redirect pages. If any of those change, the code must be updated.
Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-login-form
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 15 replies, has 3 voices.
Last updated by 5 years ago.
Assisted by: Christian Cox.