Skip to content

Instantly share code, notes, and snippets.

@dharmastyle
Last active August 29, 2015 14:14
Show Gist options
  • Save dharmastyle/c2882e2c7f682ed66b56 to your computer and use it in GitHub Desktop.
Save dharmastyle/c2882e2c7f682ed66b56 to your computer and use it in GitHub Desktop.
<?php
define('WP_USE_THEMES', false);
require( dirname(__FILE__) . '/wp-load.php' );
/**
* Return WPML defined language codes
* @return Array
*/
function getAvailableLanguages(){
$langs = icl_get_languages();
$langs = array_keys($langs);
return $langs;
}
/**
* Return available client language choosen from $availableLanguages array
*/
function getClientLanguage($availableLanguages = null, $default='en'){
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$langs=explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
//start going through each one
foreach ($langs as $value){
$choice=substr($value,0,2);
if(in_array($choice, $availableLanguages)){
return $choice;
}
}
}
return $default;
}
header("Location: ". site_url() ."/". getClientLanguage( getAvailableLanguages() ), true, 302);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment