1

I use the default login form. I want the users with certain role be redirected after succesfull loggin to the home page, but in the language that user is using in the moment of loggin.

This is an old WP installation that uses mqTranslate to give multilanguage functionallity to WP. So there is a parameter in url that have the language value in the case of second language and also in the login, register and password lost forms in order to show every user the adequate language form. When a user succesfully logged-in I need to redirect him to home in his/her language.

Here are the filters to generally construct links to the forms in the user choosed language:

add_filter( 'login_url', 'my_login_page', 10, 2 );
function my_login_page( $login_url, $redirect ) {
    $redirect = qtrans_getLanguage();
    if($redirect == '') $redirect = $_GET['lang'];
    $new_login_url = site_url() . '/wp-login.php?lang=' .  $redirect;
    return $new_login_url;
}

add_filter( 'register_url', 'my_reg_page' );
function my_reg_page( $register_url ) {
    $lang = qtrans_getLanguage();
    if($lang == '') $lang = $_GET['lang'];
    return site_url() . '/wp-login.php?action=register&lang=' . $lang;
}

add_filter( 'lostpassword_url', 'my_lostpwd_page' );
function my_lostpwd_page( $lostpassword_url, $redirect = 'es') {
    $redirect = qtrans_getLanguage();
    if($redirect == '') $redirect = $_GET['lang'];
    return site_url() . '/wp-login.php?action=lostpassword&lang=' . $redirect;
}

Till here all is correct, my problem is when I try to redirect the login form after successfull logged-in to the home page in the language desired.

// Redirect non-admins to the homepage after logging into the site.
function aux_login_redirect( $redirect_to, $request, $user  ) {
    if($_GET['lang'] === 'en') :
        $langvar = 'en';        
    elseif($_GET['lang'] === 'es') : 
        $langvar = 'es';
    else :
        $lang = qtrans_getLanguage();
        $langvar = $lang;
    endif;
    return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : site_url() . "/?lang=" . $langvar; 
} 
add_filter( 'login_redirect', 'aux_login_redirect', 10, 3 );

As I can see in my tests the login_redirect is fired twice, first time when the user arrive to the login page, in this moment the filter still can read the $_GET['lang'] variable, but when the user fills the form and click de button to loggin, then the filter is fired per second time and then the url has lost the variables and there is not doing the redirection to the desired home page.

http://domain.com/wp-login.php?lang=en must redirect to http://domain.com/?lang=en

I have tried also with $_SESSION or global variables, but as the filter is called before the login is send and after, loading the page without the variables in second timte, all information addittional is lost.

I have disabled all plugins and tried without them and the problem persist. I'm not sure if the defauult behaviour includes the twice loading of login form, and, in any case, It would be nice if anyone has any suggerence about how to achieve the desired redirection.

Thanks in advance

  • When you say old, what version is it? – Andrew Bartel Dec 2 '14 at 2:48
  • Sorry, with "old" I want to say that is not a recent development, but the WP version is the latest: 4.0.1, PHP version is 5.3.8 and MySQL version 5.0.8 – nuriarai Dec 3 '14 at 18:09
1

I ended up encoding the redirect url. using php in my case ('http://example.com/wp-login.php?redirect_to='.urlencode(http://example.com/specificpage/?parameter1=dog&parameter2=dog&parameter3=cat));)

Now it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.