![]() ![]() ![]() WPML LANGUAGE SWITCHER CODEIn the code snippet below, an array is defined with indexes as languages codes and with sub-array with countries speaking that particular language. This array is then serialized and stored into constant variable.Įnglish (en), French(fr), Italian(it), German (de) and Spanish (es) speaking country list is stored in an array. $countries_to_show = array (ĭefine('WPTS_COUNTRIES',serialize ( $countries_to_show )) Note: This is not a comprehensive list of countries speaking a specific language, for article purposes I have selected few countries per specific language. S tep 2: Now we write a shortcode for displaying above countries. But before that, under “ WPML > Languages” menu there is an option for setting language URL format. add_shortcode('wpts_countries','wpts_countries') This option is for configuring which URL format should be used to display language specific content and three URL formats are available,įor this article purpose, we are using 1 st option. The above code section declares a shortcode for displaying simple country dropdown. The global variable “ ICL_LANGUAGE_CODE” is WPML specific global variable which contains current language selection, which means if the English language is selected, ICL_LANGUAGE_ CODE variable will contain value “en”’ and so on. $countries = unserialize(WPTS_COUNTRIES) Un-serialze global constant WPTS_COUNTRIES The function getContryDropDown is defined as below, function getContryDropDown($lang = null) In above function, first, we unserialize global constant WPTS_COUNTRIES and store into $countries variable as an array. #WPML CUSTOM LANGUAGE SWITCHER SHORTCODE CODE#Īfter that, we retrieve the corresponding list of countries in switch statement for selected language code ($lang) passed as parameter and store that list in variable $show_countries.Īfter retrieving language list we loop through the array to build a drop-down box as shown is above code snippet. Now Let’s create a page and put shortcode on that page. #WPML CUSTOM LANGUAGE SWITCHER SHORTCODE CODE#ĭuplicate the page for all the languages as shown in below screen shot, this is important since if the page is not duplicated the language switcher will not work (as per WPML settings ).#WPML CUSTOM LANGUAGE SWITCHER SHORTCODE UPDATE#.#WPML CUSTOM LANGUAGE SWITCHER SHORTCODE HOW TO#. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |