Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nilsbosman/06e87b13c50011339ef8b0ea6cc94e74 to your computer and use it in GitHub Desktop.
Save nilsbosman/06e87b13c50011339ef8b0ea6cc94e74 to your computer and use it in GitHub Desktop.
TranslatePress custom language switcher
<?php
// Custom language switcher for TranslatePress
$trp = TRP_Translate_Press::get_trp_instance();
$trp_url = $trp->get_component( 'url_converter' );
$trp_settings = $trp->get_component( 'settings' )->get_settings();
// Get all published languages
$lang = $trp_settings['publish-languages'];
// Prepare empty html output array.
$output = array();
// Loop over the pusblished languages
foreach ( $lang as $key => $value ) {
// Add .active class if language is the current language.
$class_list;
if ($current_language = get_locale() == $value ) {
$class_list = 'active';
}
// Prepare the anchor tag html. Uses the language slug as front-end name.
$output[] = '<a class="'.$class_list.'" href="'.$trp_url->get_url_for_language( $value, false ).'">'. strtoupper($trp_url->get_url_slug( $value, false )) .'</a>';
// Reset class list for next item.
$class_list = '';
}
// Echo with a slash inbetween the language options NL/EN
echo implode(" / ", $output);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment