Hello, help please understand why here the cookies are empty. $lang = (isset($_COOKIE['lang'])) ? $_COOKIE['lang'] : 'nl'; get_header($lang); Below is the code that sets the cookie ob_start(); setcookie("lang", 'fr', time() + 31536000, '/'); ob_end_flush(); ob_start(); setcookie("lang", 'fr', time() + 31536000, '/'); ob_end_flush(); This part of the code is at the top of the category.php file. Cookies are set before the page loads on the previous page. After loading the page, you can see through the Chrome inspector that the cookie is a screen of chrome . Reloading the page does not help (this is not due to the fact that the cookie is not yet available or not installed). Perhaps the life cycle of the page is not the same (I just started to get acquainted with php). If you put at this moment the print of everything in the cookie, then there is only one cookie 'has_js'
var_dump($_COOKIE); // результат => array(1) { ["has_js"]=> string(1) "1" } var_dump($_COOKIE); // результат => array(1) { ["has_js"]=> string(1) "1" } which is not present when the page is already loaded through the chrome inspector screen from chrome . Thank.

  • This code is correct. Cookies are empty here because they are empty. Show the code in which cookies are set, and information about chrome cookies. - KAGG Design
  • @KAGGDesign changed the description in accordance with the code. - ovasylenko

1 answer 1

Due to the limitations of the http protocol, cookies must be transmitted before any other script data is displayed . Therefore, they will not work in the category.php file.

The correct way to send cookies from WordPress:

 add_action( 'init', 'set_lang' ); function set_lang() { setcookie("lang", 'fr', time() + 31536000, '/'); }