- #WPML CUSTOM LANGUAGE SWITCHER SHORTCODE HOW TO#
- #WPML CUSTOM LANGUAGE SWITCHER SHORTCODE UPDATE#
- #WPML CUSTOM LANGUAGE SWITCHER SHORTCODE CODE#
Duplicate 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 ). Now Let’s create a page and put shortcode on that page.
#WPML CUSTOM LANGUAGE SWITCHER SHORTCODE CODE#
After 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. In above function, first, we unserialize global constant WPTS_COUNTRIES and store into $countries variable as an array. $countries = unserialize(WPTS_COUNTRIES) Un-serialze global constant WPTS_COUNTRIES The function getContryDropDown is defined as below, function getContryDropDown($lang = null)
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. The above code section declares a shortcode for displaying simple country dropdown. 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. But before that, under “ WPML > Languages” menu there is an option for setting language URL format. S tep 2: Now we write a shortcode for displaying above countries. $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. 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. In the code snippet below, an array is defined with indexes as languages codes and with sub-array with countries speaking that particular language. We can store countries in the database table and build CRUD around it but I would keep things simple for this article. In the Child themes functions.php file, let’s define a constant for storing a list of countries. Step 1: First of all we will create a shortcode to display countries on a page. To achieve this we need to write custom code in functions.php file of “Child theme”, in our case it’s Twenty Sixteen child theme. What we want is to make countries drop down shown according to the language selected in language switcher.įor e.g let’s say if we select the French language, the country dropdown should be populated with French-speaking countries, for German language German speaking countries should be populated and so on.
#WPML CUSTOM LANGUAGE SWITCHER SHORTCODE HOW TO#
Now as shown in Screenshot 1, country drop down shows all the list of the countries populated (I will go through how to populate this dropdown field). We show the switcher in “ Theme sidebar” and here is a screenshot of the what language dropdown looks like. Multilingual Plugin WPML is installed and is configured for following languages,Īnd under “ WPML > Languages” menu there is “ Language switcher options” section where the Language switcher can be shown in many places in the theme.
#WPML CUSTOM LANGUAGE SWITCHER SHORTCODE UPDATE#
We want to update countries list in a drop-down box, according to WPML Language switcher i.e whenever the Language in “WPML Language switcher” is changed the country drop down will display all the countries speaking that specific selected language. The situation for this article is, let’s say we have the country list displayed in a drop down as shown in Screenshot 1 and also WPML Plugin is installed and configured for five different Languages as shown in Screenshot 2.