Skip to content

Instantly share code, notes, and snippets.

@ossvn
Last active May 6, 2018 19:52
Show Gist options
  • Save ossvn/770fb85b92d3ce7c8875 to your computer and use it in GitHub Desktop.
Save ossvn/770fb85b92d3ce7c8875 to your computer and use it in GitHub Desktop.
Get active languages in WPML, It's used in a plugin called "WooCommerce Checkout Advanced Fields and Templates"
<?php
/**
* Get active languages in WPML
* @return array
* @since 1.0
*/
public static function wct_get_languages(){
$languages_arr = array();
$wct_wpml = get_option('wct_wpml');
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if (is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) ) {
$languages = icl_get_languages('skip_missing=0&orderby=code');
if($languages){
foreach($languages as $l){
$languages_arr[] = array('active'=> $l['active'], 'default_locale' => $l['language_code'], 'translated_name' => $l['translated_name'], 'country_flag_url' => $l['country_flag_url']);
}
}
}
return $languages_arr;
var_dump($languages_arr);die;
}
* Get active languages in WPML - Second Method - got it from WPML core
* @return array
* @since 1.0
*/
global $sitepress;
$list_lang = $sitepress->get_active_languages();
?>
@ossvn
Copy link
Author

ossvn commented Jun 15, 2015

This is the snippet to get "active" languages in WPML, you can more information about that active languages by include more fields into the $language_arr . Available information can be added to array:

id: Internal reference id
active: This is the currently active language (exactly one language is active)
native_name: The native name of the language (never translated)
translated_name: The name of the language translated to the currently active language
country_flag_url: The URL to a PNG image with the country flag
url: The link to the translation in that language
missing: 1 if the translation for that element is missing, 0 if it it exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment