Hello. There is a site where you need to organize the choice of the region. In php I'm new, maybe it will be a stupid bydlokod, better correct. How do I do: first we get the region that the user clicked on and output it in the URL:

<div id="rega"><a href="#" title="Vitebsk">Витебск</a> <a href="#" title="Mogilev">Могилев</a></div> $('#rega a').click(function() { var region = $(this).attr('title'); window.location.href = 'http://wp/?region=' + region; }); 

Then we get its name using PHP, save it in cookies and try to send the raw header:

 <?php $defaultRegion = "Минск"; if(isset($_GET[region])) { $myRegion = $_GET[region]; setcookie("region", $myRegion, time()+3600); //Устанавливаем куки на регион header("Location: http://".$_SERVER['HTTP_HOST']."/".$myRegion); } ?> 

Writes

Warning: Cannot modify header - it has already sent by (output started at ... \ header.php: 5) in ... \ header.php on line 28

How to achieve the necessary functionality, so that the user clicked on the link, the region was selected and it was saved in the cookie and the region did not change upon the subsequent location on the site?

    4 answers 4

    In any case, headers should be sent strictly to any output. Including whitespace or error output.

    In your case, try starting with a check for cookies and a get request. And only then display a list of cities / regions

    • Also, in order to send a header, you must first receive what is sent. In my case, this is $ myRegion. UPD: Writes that the headers are already sent in the pluggable.php file ... - euqen
    • <? php if (isset ($ _ GET [region])) {$ myRegion = $ _GET [region]; setcookie ("region", $ myRegion, time () + 3600); // Set cookies to the region header ("Location: http: //". $ _ SERVER ['HTTP_HOST']. "/". $ MyRegion); } // Everything else - knes
    1. First of all, why are you doing a redirect (GET request) using JS? This can be done in the link itself:

       <a href="http://wp/?region=Vitebsk" title="Vitebsk">Витебск</a> 
    2. Secondly, if you want to do these manipulations without reloading the page, you can do this with ajax tools by sending data to a php handler, in which the cookies will be installed

    3. And thirdly, since you are using jQuery, you can set cookies simply by using the jquery cookie plugin
    • 1) I also had to give the region selected by the user php script. How can I give it directly from the link? 2) You can without it. 3) But thanks for that. - euqen

    one)

     header("Location: http://".$_SERVER['HTTP_HOST']."/".$myRegion); // было myRegion exit(); // это было пропущено 

    2) Since this is WordPress, you need to use wp_redirect (), and not amateur.

    • I do not know, I tried wp_redirect () the problem remained. UPD: Writes that the headers are already sent in the pluggable.php file ... - euqen
    • After wp_redirect () is exit () or die ()? If so, then there is some conclusion up to this point. More precisely - loading of the topic has already gone, judging by the header.php - user6550
    • is exit (); - euqen
    • Well then, the simple question is: where does this happen? Because: "Please note: it’s not a rule. A good place to call a region check is a wp_init hook: codex.wordpress.org/Plugin_API/Action_Reference/init (Runs after WordPress triggers) - user6550

    WordPress has a feature. He can connect service files without our knowledge. Therefore it is better to work with JS and redirect on JS.

    • It is necessary to read the code and not to engage in amateur. Then WP will not seem to do something "without the knowledge." Especially since some actions bypassing the API can seriously break all the work. - user6550