WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I haven't done a lot of work with cookies or Wordpress before so I think I've been a bit over-optimistic in my quest :p I have created two themes for my site, night and day. I would like the user to be able to choose which theme they'd like from a php-enabled text widget in the sidebar, which will then set a cookie with their choice, change to the appropriate stylesheet (style.css for night, the default, and style1.css for day), and load in a new header image.

I have the functionality -sort of- in place here, http://somethingoriginal.net, however the cookie does not appear to be setting immediately in Chrome as I had expcted, the echo statements I've used to print out variables for testing don't seem to be consistent, it takes a few clicks and then eventually changes, but the header image does not budge at all. In Firefox it sometimes appends a backslash to the end of my URL and this sends the user to a "not found" page. I have no idea what it does in IE.

I'm just wondering what I need to do to improve functionality? I don't -need- this, I just would like to try and implement it as I've made the two images :)

PHP sidebar text widget:


    if (isset($_COOKIE["chosenStyle"]))
    echo "Your current theme is ".$_COOKIE["chosenStyle"].", enjoy!";

    else if (isset($_POST['styles']))
    echo  "Your current theme is". $_POST['styles'].", enjoy!";

    echo "Your current theme is night, enjoy!";

EDIT: I have now updated my functions/header files and included the new code after reading this http://stackoverflow.com/questions/5936054/php-set-cookie-issue. The page CSS now updated automatically, I jsut need to change my text widget to reflect the choice. I am still having the header issue however

Functions.php file

function setDayHeader(){
         //Set header to day header
         $args = array(
         'width'         => 1000,
         'height'        => 288,
         'default-image' => get_template_directory_uri() . '/images/headers/SomethingOriginalSun.png',
        add_theme_support( 'custom-header', $args );    

function setNightHeader(){
         $args = array(
         'width'         => 1000,
         'height'        => 288,
         'default-image' => get_template_directory_uri() . '/images/headers/SomethingOriginalTheMoonAndStars.png',
         add_theme_support( 'custom-header', $args );       

function getStyles() {
echo '<form method="post" class="styles" name="styles" action="\">
        <select name="styles">
        <option value="night">Night</option>
        <option value="day">Day</option>
        <button class="submit" name="userStyleChoiceForm" onclick="submit">Set style</button></form>';

    //Set a cookie for the stylesheet   
    if (isset($_POST["styles"])) { 
        $chosenStyle = ($_POST["styles"]); 
        setcookie("chosenStyle", $chosenStyle ,time()+31536000, "");
        echo "I have set a cookie with the value ".$_COOKIE["chosenStyle"]; 


   <!-- if cookie is set, check here and then change style sheet based on cookie -->
if (!(isset($_POST["styles"]))) { //if post not set (first visit)

    if (!(isset($_COOKIE["chosenStyle"])) || ($_COOKIE["chosenStyle"]) == "night") { //if cookie not set, or is night theme ?>
         <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
    else { //cookie must be set to day theme, use day ?>
         <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/style1.css" type="text/css">

else { //if post is set 

    if (($_POST["styles"]) == "day") { //if they have chosen the day theme ?>
         <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/style1.css" type="text/css"> <?php

    else if($_POST["styles"] == "night") { //if they have chsoen the night theme ?>
     <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />  <?php
 } ?>

I tried to set the cookie in the header.php file however I then got the 'modify headers' error :|

I'm not 100% sure if I'm even referencing the right $_POST variables, and have tried combining a number of tutorials I've found, so any help would be appreciated! I realise I'm working off my main site at the minute but it's not quite 'launched' yet anyway, although I'm hoping to create a dummy/test WP site soon so I can do all this testing elsewhere. Thanks

share|improve this question
You could try to use output buffering to avoid the Headers info , or integrate the code in headers.php BEFORE sending the actuall HTTP header – krembo99 Mar 4 '13 at 1:57

I know this is an old post and you have probably already found an answer, but here goes...

When setting cookies, you need to include it in the HTTP headers as explained here: http://php.net/manual/en/function.setcookie.php

This means that you need to add a Wordpress action hook similar to as follows in your functions.php file (untested) before the headers are sent.

I have used 'template_redirect', but you could read into using 'init' also.

add_action( 'template_redirect', 'set_cookie');
function set_cookie() {
    // Set cookie 
    if (isset($_POST["styles"])) {
        setcookie("chosenStyle", $chosenStyle ,time()+31536000, "");
        wp_redirect( home_url() ); // Redirect back to home page 

Have a read about Wordpress actions/filters and this page: http://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.