Search code examples
phpwordpresswpml

Wordpress login redirect user based on language


I have a multilanguage wordpress membership site, with s2member and wpml plugins.

an example for the login page in Portuguese is: http://example.com/wp-login?lang=pt-br

I want to redirect users after the login to the Portuguese welcome page, and not the default welcome page which is English.

Is there a way to add a parameter into the login page in wordpress? If so, how can I access this parameter for my redirect?


Solution

  • Took me a while but this is how I solved it.

    I have a field in my database for the language of each user.

    I added this to the functions.php:

    add_action( 'wp', 'analyze_form_submit' );
    
    function analyze_form_submit() {
    
        global $current_user;
    
        if (is_page(86)) {  // 86 is the welcome page after login in my case
    
           $affil_id =  $current_user->user_login;
           $query="SELECT lang FROM affil WHERE affil_id=:affil_id";
           $stmt = $conn->prepare($query);
           $stmt->bindParam(':affil_id', $affil_id, PDO::PARAM_STR);
           $stmt->execute(); 
           $row = $stmt->fetch(PDO::FETCH_ASSOC);
    
           $redirect = "http://example.com/welcome?lang=";
    
           switch($row['lang']) {
              case "ENG" : $redirect .= "en"; break;
              case "HEB": $redirect .= "he"; break;
              case "ES" : $redirect .= "es"; break;
              case "BR" : $redirect .= "pt-br"; break;
              default   : $redirect .= "pt-br"; 
           };
    
           wp_redirect( $redirect );
           exit;
      };
    };
    

    I hope it will help someone in the future.