Hello, working with wordpress for the first time. I need to make the template and the data inside it switch by pressing the button, they say winter / summer

I decided to send a get request and, depending on it, set a cookie on which I can then write the conditions for the withdrawal, so I pass it this way,

<a href="<?php echo bloginfo('url')."?weather=summer"?>"> Лето </a> 

in functions.php

 function codenode_set_weather(){ $weather = $_GET['weather']; switch($weather){ case "summer": case "winter": setrawcookie('weather', $weather, time()+3600 * 3); break; default: break; } } add_action('init', $codenode_set_weather); 

and it seems to me that this is a very clumsy way of a crutch, can it be done more accurately? PS And by the way for some reason it does not work did not move in yet why

    2 answers 2

    and it seems to me that this is a very clumsy way of a crutch, can it be done more accurately?

    Welcome to WordPress. As far as I know, there is no additional convenient function for working with cookies, and the logic itself is too simple, everything is fine, except for the switch itself:

     if (isset($_GET['weather']) && in_array($_GET['weather'], array('summer', 'winter')) { setcookie('weather', $_GET['weather'], time() + (3600 * 3)); } 
    • Thank you for your answer, and what do you think is bad in the switch? - 0xdef 10:51 pm
    • @ 0xdef, it is logically inapplicable here, and the operation performs rather strange for a switch. The same functionality. - etki

    I would like to clarify: the administrator changes the data or the user, by pressing the button?)

    At the moment I see myself the implementation of this task using two templates. It is much more convenient to give the user the choice of a template than to reinvent the wheel.

    • The user changes by pressing the button, changes the background image, slideshow, gallery. a lot of code duplication will be if you duplicate the entire template. Where to look to understand the "give the user a choice of 2 templates" - 0xdef
    • sooource.net/wordpress-theme-switcher is the first option that I found in google. Just using 2 themes makes it unnecessary for you to use additional crutch functions. I am for simplicity and elegance ... - cema93