Created
February 11, 2021 12:58
-
-
Save nilsbosman/06e87b13c50011339ef8b0ea6cc94e74 to your computer and use it in GitHub Desktop.
TranslatePress custom language switcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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