| 1 |
``` |
| 2 |
<?php |
| 3 |
/** |
| 4 |
* WPGlobus language switcher. |
| 5 |
* Example 1: with using module `Publish` from WPGlobus Plus. |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly |
| 10 |
} |
| 11 |
|
| 12 |
if ( class_exists( 'WPGlobus' ) ): ?> |
| 13 |
<div class="wpglobus-selector-box"> <?php |
| 14 |
|
| 15 |
/** |
| 16 |
* Filter that prevent using language that has `draft` status. |
| 17 |
* That works with module `Publish` from WPGlobus Plus add-on. |
| 18 |
*/ |
| 19 |
$enabled_languages = apply_filters( 'wpglobus_extra_languages', WPGlobus::Config()->enabled_languages, WPGlobus::Config()->language ); |
| 20 |
|
| 21 |
foreach ( $enabled_languages as $language ): |
| 22 |
$url = null; |
| 23 |
$is_current = true; |
| 24 |
|
| 25 |
if ( $language != WPGlobus::Config()->language ) { |
| 26 |
$url = WPGlobus_Utils::localize_current_url( $language ); |
| 27 |
$is_current = false; |
| 28 |
} |
| 29 |
|
| 30 |
$flag = '<img src="'.WPGlobus::Config()->flags_url . WPGlobus::Config()->flag[ $language ].'" />'; |
| 31 |
$link = $flag . ' ' . WPGlobus::Config()->en_language_name[$language] . ' '; |
| 32 |
|
| 33 |
printf( '<a %s %s>%s</a>', ( empty( $url ) ? '' : 'href="' . esc_url( $url ) . '"' ), ( $is_current ? 'class="wpglobus-current-language"' : '' ), $link ); |
| 34 |
|
| 35 |
endforeach; ?> |
| 36 |
|
| 37 |
</div> <?php |
| 38 |
endif; |
| 39 |
|
| 40 |
/** |
| 41 |
* WPGlobus language switcher. |
| 42 |
* Example 2: for two languages and active language is hidden. |
| 43 |
*/ |
| 44 |
if ( class_exists( 'WPGlobus' ) ): |
| 45 |
|
| 46 |
$wpglobus_language_image = array( |
| 47 |
'en' => array( |
| 48 |
'src' => 'https://wetag.io/wp-content/plugins/language-icons-flags-switcher/img/english.png', |
| 49 |
'alt' => 'English', |
| 50 |
'title' => 'English' |
| 51 |
), |
| 52 |
'vi' => array( |
| 53 |
'src' => 'https://wetag.io/wp-content/plugins/language-icons-flags-switcher/img/Vietnam.png', |
| 54 |
'alt' => 'Vietnam', |
| 55 |
'title' => 'Vietnam' |
| 56 |
) |
| 57 |
); |
| 58 |
echo '<div class="wpglobus-language-switcher">'; |
| 59 |
foreach( WPGlobus::Config()->enabled_languages as $language ) { |
| 60 |
if ( $language == WPGlobus::Config()->language ) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
echo '<a href="' . WPGlobus_Utils::localize_current_url( $language ). '" class="">'; |
| 64 |
echo '<img alt="'.$wpglobus_language_image[$language]['alt'].'" title="'.$wpglobus_language_image[$language]['title'].'" src="'.$wpglobus_language_image[$language]['src'].'" />'; |
| 65 |
echo '</a>'; |
| 66 |
} |
| 67 |
echo '</div>'; |
| 68 |
|
| 69 |
endif; |
| 70 |
|
| 71 |
/** |
| 72 |
* WPGlobus language switcher. |
| 73 |
* Example 3: If you need to hide extra languages from the switcher on the category or tag page. |
| 74 |
* |
| 75 |
* You can add this code to your active theme's `functions.php` file. |
| 76 |
* Please note the information about the child theme |
| 77 |
* https://developer.wordpress.org/themes/advanced-topics/child-themes/ |
| 78 |
*/ |
| 79 |
if ( class_exists( 'WPGlobus' ) ): |
| 80 |
|
| 81 |
add_filter( 'wpglobus_extra_languages', 'filter__extra_languages', 5, 2 ); |
| 82 |
function filter__extra_languages( $extra_languages, $current_language ) { |
| 83 |
if ( is_tag() || is_category() ) { |
| 84 |
return array(); |
| 85 |
} |
| 86 |
return $extra_languages; |
| 87 |
} |
| 88 |
|
| 89 |
endif; |
| 90 |
|
| 91 |
/** |
| 92 |
* WPGlobus language switcher. |
| 93 |
* Example 4: You can remove the language switcher from a particular page at all. |
| 94 |
* @since WPGlobus v.2.10.2 |
| 95 |
* |
| 96 |
* You can add this code to your active theme's `functions.php` file. |
| 97 |
* Please note the information about the child theme |
| 98 |
* https://developer.wordpress.org/themes/advanced-topics/child-themes/ |
| 99 |
*/ |
| 100 |
if ( class_exists( 'WPGlobus' ) ): |
| 101 |
|
| 102 |
add_filter( 'wpglobus_disable_switcher', 'filter__disable_switcher', 5, 3 ); |
| 103 |
function filter__disable_switcher( $disable_switcher, $current_language, $nav_menu ) { |
| 104 |
|
| 105 |
if ( is_tag() ) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
return $disable_switcher; |
| 110 |
} |
| 111 |
|
| 112 |
endif; |
| 113 |
``` |
| 114 |
|