Read cookie in header template?
-
Hello!
Though my problem is probably quite simple, I did not find a previous post that really matches, but please apologize if I’ve missed something.I want to read a cookie and load a stylesheet according to the value. It’s a styleswitcher I made myself some time ago, I already have a bunch of stylesheets that create completely different layouts while the html code is always the same. If it is possible, I wish to keep it and not use another style or theme switcher.
It worked fine with WP 1.2.2. I created the html markup I needed via the index.php and wp-comments.php. In the part that creates the header in the index.php, I checked if there is a cookie and loaded the according css file. If there was no cookie, the default css file was loaded.
It should work with WP 1.5.1.3 too. I already made a theme that generates the html markup I need, no problem with that. But unfortunately WP seems not to read the cookie. The cookie is there and the value too, it’s set by a php script outside WP via a form where the user can choose the layout. Pages outside WP just do what I want them to do, they load the css file according to the cookie, but all WP pages just load the default css file as if there was no cookie.
Here is the code I inserted in the header template where you usually load the theme’s css file, $stylename is the variable in the cookie:
<?php
if(!$stylename)
{
echo "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"default.css\" />\n";
}
else
{
switch ($stylename)
{
case style1:
echo "\t<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"style1.css\" />\n";
break;
case style2:
echo "\t<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"style2.css\" />\n";
break;
}
}
?>Any idea why this does not work?
- The topic ‘Read cookie in header template?’ is closed to new replies.