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:
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 included the new code after reading this https://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>
</select>
<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"];
}
Header.php
<!-- if cookie is set, check here and then change style sheet based on cookie -->
<?php
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="https://wordpress.stackexchange.com/questions/89186/<?php bloginfo("stylesheet_url'); ?>" type="text/css" />
<?php
setNightHeader();
}
else { //cookie must be set to day theme, use day ?>
<link rel="stylesheet" href="https://wordpress.stackexchange.com/questions/89186/<?php bloginfo("template_url'); ?>/style1.css" type="text/css">
<?php
setDayHeader();
}
}
else { //if post is set
if (($_POST["styles"]) == "day") { //if they have chosen the day theme ?>
<link rel="stylesheet" href="https://wordpress.stackexchange.com/questions/89186/<?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="https://wordpress.stackexchange.com/questions/89186/<?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