Skip Navigation

[Resolved] Conditional login in different languages (with WPML enabled)

This thread is resolved. Here is a description of the problem and solution.

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 support ticket is created 4 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

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)

Tagged: 

This topic contains 15 replies, has 3 voices.

Last updated by simonM-5 4 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1383149

Tell us what you are trying to do?
Hello there again

I just entered this ticket before but when I clicked Submit I saw "ERROR: are you sure you wanted to do that?". Slightly annoying, now I have to retype my ticket... Anyway, here goes again:

On our site we basically have 2 types of users, Family and Nannies.

When they login, we want to redirect them to different pages, depending on their Access role.
Login page (English): hidden link
User role family should be redirected to hidden link
User role native_nanny should be redirected to hidden link

Following the example found here:
https://toolset.com/forums/topic/login-form-redirect-issue/

I added our custom code to Toolset > Settings > Custom Code > conditional-redirect-on-successful-login.

This works great for the English site. However our site uses WPML and has German and Spanish equivalents.

For our German visitors it should be like:
Login page (German): hidden link
User role family should be redirected to hidden link
User role native_nanny should be redirected to hidden link

At the moment, when the user starts in German, they are redirected to the /home_url/en/find-a-native_nanny page when logged in.
The same will happen in Spanish and any other future languages we might implement in the future...

Is there a way round this, ie so that Germans always stay on the German pages and don't get redirected to the English equivalents?

Thanks and regards
Simon

Is there any documentation that you are following?
https://toolset.com/forums/topic/login-form-redirect-issue/

Is there a similar example that we can see?
hidden link

What is the link to your site?
hidden link

#1383339

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2019-11-14 at 10.14.04 AM.png

Hi Simon,

Thank you for getting in touch.

There should be an automatic language redirect in WPML that would resolve this for you. See Screenshot

Go to WPML -> Language -> Browser Language redirect .

This should allow you to setup the browser redirect based on the user's language.

Thanks,
Shane

#1383355
Screenshot 2019-11-14 at 16.24.01.png
Screenshot 2019-11-14 at 16.23.07.png
Screenshot 2019-11-14 at 16.25.16.png

HI Shane

I have both pages translated (the Login page, and the destination page), see screenshots. Also, I already had WPML set as in the screenshot to redirect already, but only if translations existed, which they do. So I'm puzzled as to why it is still not behaving as I would expect...

Thanks and regards
Simon

#1383491

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

The problem could be here .
https://toolset.com/forums/topic/login-form-redirect-issue/

The code that I had provided doesn't take into consideration the language of the page.

It would need to be modified to take into consideration the language of the page.

Does the user get redirected to the correct language login page when they access it ? Meaning if I go to the login page in English but my browser is in spanish, does it redirect you to the spanish login page?

Please let me know.

Thanks,
Shane

#1384095

HI Shane

Yea, I got the code from that page you just mentioned there. I understand that to a certain degree we are hardcoding where a user should be navigated to when they log in. The current situation is as you describe it, ie the site should check which is the standard language of the browser and then direct you to either dev.native-nanny.com/en /de or /es. I'm not 100% sure where that setting was again, but I do seem to remember setting it somewhere way back when i set the site up and my colleague uses a German browser and always lands by default on the German pages.

In other words, if German is the standard language of the browser and I type dev.native-nanny.com into the URL, then I will get navigated to dev.native-nanny.com/de automatically. The default language of the website is /en.

You write It would need to be modified to take into consideration the language of the page.
>> How would we go about this? I'm not a PHP developer so couldn't write this myself.

Alternatively, what I did try however already was to wrap the redirect_url from the login form in conditional tags:
{!{wpv-login-form redirect_url='hidden link' redirect_url_fail='hidden link' allow_remember='true'}!}

Here I wanted to say "if user logs in successfully and has role family, then go here" or "if user logs in successfully and has role native_nanny, go there", but it didn't work for me. Perhaps it is possible but I screwed it up somehow syntactically. If this were possible we would need 7 "case statements":
1) if I'm a Nanny using German browser, go here ... de/...
2) if I'm a Nanny using Spanish browser, go here ... es/...
3) if I'm a Nanny using English or non-German and non-Spanish browser, go here ... en/...
4) if I'm a Family using German browser, go here ... de/...
5) if I'm a Family using Spanish browser, go here ... es/...
6) if I'm a Family using English or non-German and non-Spanish browser, go here ... en/...
7) else if I'm an admin, go to the WP Dashboard

Is this feasible? It would also mean that we wouldn't have to have any Code Snippets, since we could handle all this on the front end in the log in form....

Thanks and regards
Simon

#1384523

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

Would you mind allowing me to have admin access to your site so that I can make a few changes to the code to account for this ?

Thanks,
Shane

#1386023

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

I've made the modification to the code


function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
  $my_current_lang = apply_filters( 'wpml_current_language', NULL );
    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 ) && $my_current_lang == 'en' ) {
            return home_url('/find-a-native-nanny/');
        } elseif ( in_array( 'family', $user->roles ) && $my_current_lang == 'es' ){
            return home_url('/es/find-a-native-nanny/');
        }elseif ( in_array( 'family', $user->roles ) && $my_current_lang == 'de' ){
            return home_url('/de/find-a-native-nanny/');
        }elseif( in_array( 'native_nanny', $user->roles ) && $my_current_lang == 'en' ){
			return   home_url('/find-a-job/');
		}elseif( in_array( 'native_nanny', $user->roles ) && $my_current_lang == 'es'  ){
			return   home_url('/es/find-a-job/');
		}elseif( in_array( 'native_nanny', $user->roles ) && $my_current_lang == 'de'  ){
			return   home_url('/de/find-a-job/');
		}
    } else {
        return $redirect_to;
    }
}
  
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Please try now and let me know if the issue is resolved.

Thanks,
Shane

#1386061

Hi Shane

Thanks for that. Unfortunately it hasn't worked yet, but I think we're very close.

I made minor changes to your code, where i put the slugs of the German and Spanish pages in

The code now looks like:
if ( in_array( 'administrator', $user->roles ) ) {
// redirect them to the default place
return $redirect_to;
} elseif ( in_array( 'family', $user->roles ) && $my_current_lang == 'en' ) {
return home_url('/find-a-native-nanny/');
} elseif ( in_array( 'family', $user->roles ) && $my_current_lang == 'es' ){
return home_url('/es/encuentra-tu-nanny/');
}elseif ( in_array( 'family', $user->roles ) && $my_current_lang == 'de' ){
return home_url('/de/native-nanny-finden/');
}elseif( in_array( 'native_nanny', $user->roles ) && $my_current_lang == 'en' ){
return home_url('/find-a-job/');
}elseif( in_array( 'native_nanny', $user->roles ) && $my_current_lang == 'es' ){
return home_url('/es/find-a-job/');
}elseif( in_array( 'native_nanny', $user->roles ) && $my_current_lang == 'de' ){
return home_url('/de/encuentra-tu-trabajo/');

However that didn't work either.

1) The session variable && $my_current_lang == 'en' - what is it actually querying? The default language of the operating system or the default language of the browser?

2) Why do we only need the /de/ and /es/ for German and Spanish but we don't need /en/ for English? Is that because the default language is en and the others are just translated languages?

Kind regards
Simon

#1386361

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

"1) The session variable && $my_current_lang == 'en' - what is it actually querying? The default language of the operating system or the default language of the browser?"

This is a variable that i use to store the returned language with the filter hook that wpml provides. So I can get this to see what is the currently selected language that the user made on the site.

So if the user visit the login page in spanish then the variable will return spanish. So if they log in user the spanish log in page they should get redirected correctly.

"2) Why do we only need the /de/ and /es/ for German and Spanish but we don't need /en/ for English? Is that because the default language is en and the others are just translated languages?"

I believe this is because english is your default language so it doesn't need to have the /en on it.

Please let me know if this helps.

Thanks,
Shane

#1386411

HI Shane

This helps from an informational point of view, but it still not working, either with your code or my slightly modified code (where I only changed the page slugs for de and es).

What does the software expect? The English (default) page slugs or the German/Spanish page slugs?

Thanks and regards
Simon

#1387121

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

I found the issue. So the original plan of redirecting them based on language won't work because it defaults to English in the transitional phase when logging in.

What you are going to have to do is a language redirect after they have landed on the English page using the default WPML redirect settings.
https://wpml.org/documentation/getting-started-guide/language-setup/automatic-redirect-based-on-browser-language/

Thanks,
Shane

#1387781
Screenshot 2019-11-20 at 10.08.59.png

HI Shane

We had to deactivate the code you modified recently, because I was not even able to log in any more as an admin. My browser page was simply returning a completely white page with "en", and I wasn't able to see the WP Admin dashboard. Luckily my colleague was already in the WP Admin in an old session and was able to access the Toolset > Settings > Custom Code tab and deactivate the code snippet. Now I can access the WP Admin again.

We retested this morning, all translations exist for the pages we want to redirect to in all 6 "cases", and I believe we have set up WPML correctly (see screenshot), however we are still missing conditional login based on role.

When we open a fresh private window in a German browser, we land correctly on dev.native-nanny.com/de.
When we open a fresh private window in an English browser, we land correctly on dev.native-nanny.com/en.

After successful login for role family the redirect functions correctly (as programmed in the [wpv-login-form], ie we always land where we want to as a family:
../en/find-a-native-nanny
../de/native-nanny-finden
../es/encuentra-tu-native-nanny

This works I believe, because the login was successful, and so it is forced to go there due to what is programmed in the
[wpv-login-form] shortcode:
[wpv-login-form redirect_url='hidden link' redirect_url_fail='hidden link' allow_remember='true']

However, when we log in for role native_nanny, we are redirected to the same page, which Nannies should not see ever, rather they should be sent to the following pages:
../en/find-a-job
../de/job-finden
../es/encuentra-tu-trabajo

In other words, we need the redirect to function thus:

if ( in_array( 'administrator', $user->roles ) ) {
// redirect them to the default place
return $redirect_to;

} elseif ( in_array( 'family', $user->roles ) && $my_current_lang == 'en' ) {
return home_url('/en/find-a-native-nanny/');
}elseif ( in_array( 'family', $user->roles ) && $my_current_lang == 'de' ){
return home_url('/de/native-nanny-finden/');
} elseif ( in_array( 'family', $user->roles ) && $my_current_lang == 'es' ){
return home_url('/es/encuentra-tu-native-nanny/');

}elseif( in_array( 'native_nanny', $user->roles ) && $my_current_lang == 'en' ){
return home_url('/en/find-a-job/');
}elseif( in_array( 'native_nanny', $user->roles ) && $my_current_lang == 'de' ){
return home_url('/de/job-finden/');
}elseif( in_array( 'native_nanny', $user->roles ) && $my_current_lang == 'es' ){
return home_url('/es/encuentra-tu-trabajo/');

I hope I was able to express our requirement clearly... 🙂

Thanks and regards
Simon

#1388031

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

The redirect should still work if you redirect here /en/find-a-job/ if you log in as Nanny in Spanish.

The WPML redirect should react when you as a spanish user lands on the English page.

The only thing we should need to do is redirect based on the user login.


function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
  $my_current_lang = apply_filters( 'wpml_current_language', NULL );
    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 home_url('/find-a-native-nanny/');
        }elseif( in_array( 'native_nanny', $user->roles )  ){
            return   home_url('/find-a-job/');
        }
    } else {
        return $redirect_to;
    }
}
   
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Pretty much WPML should take care of the language redirects relatively to the page that the user lands on. Does the redirects work for /find-a-native-nanny/ and find-a-job/' ?

We wouldn't be able to use the login redirect to redirect them based on language.

Thanks,
Shane

#1389915

HI Shane

I put in your revised code and tested. We're closer to the solution, but it's not quite there yet. 🙂

In English, everything works perfectly. As a native_nanny we are redirected to hidden link.

In de and es, the native_nanny role is always redirected to /en/find-a-job, instead of /de/job-finden and /es/encuentra-tu-trabajo, regardless of which language the user performs the login from.

For example if I log in as a native_nanny from hidden link (the German login page), I still land on hidden link instead of hidden link

Kind regards
Simon

#1391959

Hi, Shane is out this week on holiday so I'm checking in on this ticket. I created a copy of the site and ran a few tests, and the following code is working for me in all languages with both family and nanny User roles:

toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.

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 this code is very tightly coupled to the actual URLs used in the redirect settings for each login form, as well as the actual page URLs of each redirect destination. If those ever change even slightly, this code must be updated to reflect those new URLs. I have updated the custom code snippet on your site so you can test. Please let me know the results of your tests and we can go from there.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.