Using PHP to switch stylesheet and header image, set in cookie

Using PHP to switch stylesheet and header image, set in cookie

2 minutes, 25 seconds Read

I haven’t worked much with cookies or WordPress yet, so I think I’ve been a bit too optimistic in my search :p I’ve created two themes for my site, day and night. I’d like the user to be able to choose which theme they want from a php-compatible 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 a new header image.

I have the functionality here -kind of-, http://ietsorigineel.netbut the cookie doesn’t seem to be set immediately in Chrome as I expected, the echo statements I used to print variables for testing don’t seem to be consistent, it takes a few clicks and then eventually changes, but the header image doesn’t differ at all. Firefox sometimes adds a backslash to the end of my URL, sending the user to a “not found” page. I have no idea what it does in IE.

I’m just wondering what should I do to improve functionality? I don’t need this, I would just like to try and implement it the way I made the two images 🙂

PHP sidebar text widget:

    getStyles();

    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!";

    else
    echo "Your current theme is night, enjoy!";
    ?>

EDIT: I have now updated my functions/header files and added the new code after reading this https://stackoverflow.com/questions/5936054/php-set-cookie-issue. The page CSS now updates automatically, I just need to change my text widget to reflect the choice. However, I still have the header problem

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 '';
}

    //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"]; 
    }

Header.php

   

         " type="text/css" />
         
         /style1.css" type="text/css">
         
         /style1.css" type="text/css"> 
     " type="text/css" />  

I tried to set the cookie in the header.php file but then I got the error ‘Change headers’ 😐

I’m not 100% sure I’m referring to the correct $_POST variables, and have been trying to combine some tutorials I found, so any help would be appreciated! I realize I’m working outside of my main site at the moment, but it hasn’t quite ‘launched’ yet, although I hope to create a dummy/test WP site soon so I can do all this testing elsewhere. Thank you

#PHP #switch #stylesheet #header #image #set #cookie

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *