Read cookie in header template? (4 posts)

  1. Hathor
    Posted 11 years ago #

    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 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:

    echo "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"default.css\" />\n";
    switch ($stylename)
    case style1:
    echo "\t<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"style1.css\" />\n";
    case style2:
    echo "\t<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"style2.css\" />\n";

    Any idea why this does not work?

  2. Tamblyne
    Posted 10 years ago #

    Could you tell me if you got this figured out? It turns out I'm trying to do exactly the same thing -- and am having the same problem. :-)

  3. cyb3rllama
    Posted 10 years ago #

    Me too... I have the exact same problem.

  4. cyb3rllama
    Posted 10 years ago #

    This post answers it:


    I simply added this line and it works:


Topic Closed

This topic has been closed to new replies.

About this Topic