PHP – Simple Cookie Not Saving (WordPress)

I’m trying to make a language cookie, but for some reason the cookie isn’t saving.

here’s the code I’m using

Read More
if ( !empty($_GET['language']) ) {
    setcookie('language-eclear', $_GET['language']);
}


if ( empty($_COOKIE['language-eclear']) ) {
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    switch ($lang){
        case "fr":
            include("langFiles/lang-indexFR.php");
            break;
        case "de":
            include("langFiles/lang-indexDE.php");
            break;
        case "en":
            include("langFiles/lang-indexEN.php");
            break;
        case "nl":
            include("langFiles/lang-indexNL.php");
            break; 
        default:
            include("langFiles/lang-indexEN.php");
            break;
    }
}else{
    $lang = $_COOKIE['language-eclear'];
    switch ($lang){
        case "fr":
            include("langFiles/lang-indexFR.php");
            break;
        case "de":
            include("langFiles/lang-indexDE.php");
            break;
        case "en":
            include("langFiles/lang-indexEN.php");
            break;
        case "nl":
            include("langFiles/lang-indexNL.php");
            break; 
        default:
            include("langFiles/lang-indexEN.php");
            break;
    }
}
?>

the if loop for setting the cookie works I tested it by echo’ing $_GET[‘language’];.

However it seems that the cookie isn’t saving. What am I missing?

PS: I’m using a wordpress website

Related posts

Leave a Reply