Skip to main content
2 of 8
added 23 characters in body
l2aelba
  • 857
  • 15
  • 29

I got it now , After searched many resources :

add_action('init','rewrite_lang');
function rewrite_lang(){
    $langs = array('en','fr'); // Set your langs here
    foreach($langs as $lang) {
        add_rewrite_endpoint($lang,EP_PERMALINK|EP_PAGES|EP_ROOT);
    }
}

function lang(){
    global $wp_query;
    $langs = array('en','fr'); // Set your langs here too
    $lang_r = "";
    foreach($langs as $lang) {
        if(isset($wp_query->query_vars[$lang])) {
            $lang_r = $lang;
        }
    }
    if(in_array($lang_r,$langs)) {
        return $lang_r;
    } else {
        return "en"; // If lang not set in url so default is "en"
    }
}

So now you can check/use lang code in your template like..

<?php echo lang();?>

or if( lang() === "en" )

Example work urls with my example code :

http://domain.com/ <-- "en"
http://domain.com/en/ <-- "en"
http://domain.com/fr/ <-- "fr"
http://domain.com/es/ <-- "en" (coz , "es" code not set)

http://domain.com/mypage/ <-- "en" (work with "page")
http://domain.com/mypage/fr/ <-- "fr"
http://domain.com/mypage/es/ <-- "en" (coz , "es" code not set)

http://domain.com/mypost/ <-- "en" (work with "post")
http://domain.com/mypost/fr/ <-- "fr"
http://domain.com/mypost/es/ <-- "en" (coz , "es" code not set)
l2aelba
  • 857
  • 15
  • 29