I have created multiple menus. What I wish to accomplish is give viewers the ability to select which menu they want to use for the header. Closest ive gotten to is the conditional menu plugin but that is based on the page that is being viewed. I wish to enable the user to select a menu and have it apply all across the website for the whole session duration. Any ideas on how I would go around doing this or any simple plugin can already do this? Thanks in advance!
1 Answer
One way of doing this would be to set a cookie when the user selects which menu they want to view, and on all page visits, check the cookie to see which menu they chose, and display the menu that matches their selection.
A cookie can be set using JavaScript on the client side, while your PHP page template will look for the presence of the menu cookie in the $_COOKIE
superglobal. An overly simplified example is below.
Client side:
// User selects Menu 1, which executes the following line
document.cookie = "menu_type=1";
Then in the appropriate place in your theme template PHP:
if ($_COOKIE['menu_type'] === '1') {
// show menu 1 using the WP menu functions
} else {
// show default menu
}
I didn't test this code so the syntax might not be right, but this will hopefully get you pointed in the right direction.