0

i have a wordpress network site for multi language purpose now for example i have example.com -- > the main site example.com/en --> English site

example.com/fr --> french site

i want to add in the main site example.com two buttons one for English and the another for french but i want wordpress the main site remember my choice so when i go to the main site next time i don't have to choose the language again.

1 Answer 1

0

You can try using a cookie. If it's the visitor's first time, set a cookie. Let the user set the cookie once he clicks on the language he wants.

You may refer to his sample Wordpress Redirect based on the prescence of a cookie

You may tweak it a bit to do something like

 if ( !isset($_COOKIE["language"])) {
        if(!isset($_GET["language"])){ // Check if user has set a language through a get function
           setcookie('language', 1, time()+1209600, "/", "http://www,yourdomain.com/".$_GET['language']."", false); // Set the chosen cookie
           wp_redirect( get_site_url().'/'.$_GET['language'] ); exit; // redirect to the chosen language
        }else{
          setcookie('language', 1, time()+1209600, "/", "http://www,yourdomain.com/en", false); // If did not choose, just redirect to your default language
        }
    }else{
        wp_redirect( get_site_url().'/'.$_COOKIE['language'] ); exit; // Else, redirect to the language based on the cookie stored previously
    }
2
  • thank you for helping me i'm not adeveloper may ypu told me in details how to use this and my page for example have this html code <a href="site1url">site1url</a> <a href="site2url">site2url</a> -- and can i use javascript for this ? Commented Sep 1, 2017 at 21:35
  • You can do something like <a href="yourdomain,com/?language="en">English Flag Here</a> Commented Sep 1, 2017 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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