How to add a custom floating language switcher to your WordPress site style2.track360.xyz (ok)
https://wpml.org/tutorials/2020/08/how-to-add-a-custom-floating-language-switcher-to-your-wordpress-site/
Last updated
Was this helpful?
https://wpml.org/tutorials/2020/08/how-to-add-a-custom-floating-language-switcher-to-your-wordpress-site/
Last updated
Was this helpful?
Follow this tutorial to add a floating language switcher to your WordPress site with WPML. A floating language switcher looks great and makes it easier for your customers to view your site in their language.Adding a floating language switcher
You can add a custom floating language switcher to your site in three steps:
Add the PHP code to render the language switcher
Style your language switcher with CSS
Update your language switcher settings
The first step is to add the PHP code responsible for rendering the language switcher on our page. For that, we will create a function to add a <div> container with the language switcher inside it. We can use the wpml_add_language_selector action to render the language switcher.
In this example, we want the new language switcher to be displayed in the footer, so we will hook our new function to wp_footer.
With the previous code, we will already have a new language switcher added to the footer of our website. Now it’s time to customize it so that it is floating in the bottom right corner of the website, even if the user scrolls down the page. This can be done using CSS with the position: fixed attribute.
To add the CSS code:
Go to WPML → Languages
Scroll down to Language switcher options and expand the Additional CSS section.
Or, you can add the CSS code by going to Appearance → Customize and clicking Additional CSS.
Finally, we need to change some settings to have only the flags on our language switcher. To do this:
Go to WPML → Languages.
Scroll down to Custom language switchers and click Enable.
Click the Customize button.
For What to include in the language switcher, select Flag and uncheck the other options.
Click Save.
Displaying only the flag in the floating language switcher
That’s it! After these steps, you should have a nice floating language switcher on the website:The language switcher on the front-end
If you want, you can also customize it further to create a vertical language switcher, like the screenshot below:Vertical language switcher
The complete PHP code will look like this. You can copy and add it to your theme’s functions.php file.Language switcher PHP?
This example adds some extra customization like rounded borders and a box shadow, but feel free to customize it as you want.Language Switcher Styling?
For this, just replace the previous CSS with the code below:Vertical Language Switcher CSS?