Question

im working on a wordpress blog tryin to develop a multilanguage system. Whenever the user clicks on his language button the lang query parameter is added to the url

Ex. localhost/my-blog?lang=es

Everything works.The point is i have the blog main menu that has links to different sections of the site that are using the wordpress bloginfo('url') :

Ex. <a href="<?php bloginfo('url');?>/contact-us">contact us</a>

And whenever the user choose it's language at the home-page and then clicks on "contact us"

he receive this wrong link:

localhost/my-blog?lang=es/contact-us

which wordpress function you use guys for this kind of things?

thanks

Luca

Was it helpful?

Solution

I think this type of url query will get you into trouble in the future, why not conform to using either a session value or base cookie to store the users choice,

then that way you can simply add some code to your functions.php file to read the session value or cookie, and return the translation type?

there is also this plugin xili-language

ie: functions.php

// START THE SESSION
function start_session(){
  session_start();
}
add_action('init', 'start_session', 1);

function set_lang_pref($lang_pref){       
   if(isset($_GET['lang']) && ($_GET['lang']!=''){
      $setlang = $_GET['lang'];
      switch($setlang){
         case "es" :
         $lang = "es_ES";
         $_SESSION['selectedlanguage']=$lang;
         break;
      }
   }else{
      return false;
   }

}

the code is really rough, but you get the idea..? store the users choice, check for that choice,

if its been set then use that value as the language pref on the site..? else just revert back to the default..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top