I want to change primary menu according to the language of the user. I have reviewed multi language plugins such as WPML and qTranslate. They have lots of features but what I want is much simpler than what they provide.
There will be three links such as English, Arabic, and Turkish in the upper right corner. When the user clicks one of them, the menu bar should change accordingly.
This can be done via cookies.
We set it with Javascript, reload the page and display different menus in PHP using its value.
First, print the script at the site footer (see comments).
Note: would be better to enqueue this together with your theme scripts.
If jQuery is not included in your theme:
Then, in the theme
header.php
, we put this:Then, the
wp_nav_menu
:In this example, the navigation menus have the same name of the values we’ll set for the cookie. If your menus have another name, you have to adapt the header conditional for
$user_lingo
.Finally, the language menu. The class
lingo
is binded via jQuery to set the cookie value with the anchorsid
‘s. Thehref
value forces the page to reload, you can use another method if you wish.