WordPress.org

Ready to get started?Download WordPress

Forums

Read cookie in header template? (4 posts)

  1. Hathor
    Member
    Posted 9 years ago #

    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?

  2. Tamblyne
    Member
    Posted 8 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
    Member
    Posted 8 years ago #

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

  4. cyb3rllama
    Member
    Posted 8 years ago #

    This post answers it:

    http://wordpress.org/support/topic/31647

    I simply added this line and it works:

    $sitestyle=$_COOKIE['sitestyle'];

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags