291 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+3 more
AkinGn says

I commented under another thread and couldn’t get any reply, so I’m creating a new thread. I’m pasting my previous post:

I was using a javascript style switcher to change the backgrounds and it was working. Then I tried to change top navigation’s background color independent of body background, but whenever I change either of them the other one falls back to default. (ie I can’t view the alternative background with alternative navigation color) And I decided to try this php method. I pasted this code to my header.php:

<?php if(isset($_GET['color'])) { $color = $_GET['color']; setcookie('color', $color); }?>
    <?php if(isset($_GET['color']))
        {    
            $color = $_GET['color']; 
        }
        else
        {
            if(isset($_COOKIE['color']))
              {
                  $color = $_COOKIE['color'];            
              }
          else
              {
                $color = get_option('color');                   
              }
        }
    ?>

<link href="&lt;?php bloginfo( 'template_url' ); ?&gt;/css/&lt;?php echo $color; ?&gt;" rel="stylesheet" media="all" type="text/css" />

and created a list like:

<ul>
     <li><a href="<?php bloginfo('url'); ?>/?color=blue">Blue</a></li>
     <li><a href="<?php bloginfo('url'); ?>/?color=orange">Orange</a></li>
</ul>

(and I have the css files under a css folder named blue.css etc) But I get the error:

Warning: Cannot modify header information – headers already sent by (output started at …/header.php:5) in …/header.php on line 13

(And I’m trying all of this @ local host just to inform) I guess if I manage to do this, I’ll be able to change different elements’ styles independently. Anyone can help?

1012 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+6 more
wizylabs says

I optimised the code above just few, you dont need get_option('color'); in here as you can just add ?color=blue at the end of the demo url here on themeforest

if(isset($_GET['color'])) {

    $color = $_GET['color'];

    if(!isset($_COOKIE['color'])) {
        setcookie('color', $color);
    } else {
        $color = $_COOKIE['color'];
    }

} else {
    $color = get_option('color');
}

<link href="<?php bloginfo('template_directory'); ?>/css/<?php echo $color; ?>.css" rel="stylesheet" type="text/css" media="all" />
291 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+3 more
AkinGn says
I optimised the code above just few, you dont need get_option('color'); in here as you can just add ?color=blue at the end of the demo url here on themeforest
if(isset($_GET['color'])) {

    $color = $_GET['color'];

    if(!isset($_COOKIE['color'])) {
        setcookie('color', $color);
    } else {
        $color = $_COOKIE['color'];
    }

} else {
    $color = get_option('color');
}

<link href="<?php bloginfo('template_directory'); ?>/css/<?php echo $color; ?>.css" rel="stylesheet" type="text/css" media="all" />

Thanks so much wizy labs, I tested it and it works! But it keeps giving that error (Warning: Cannot modify header information – headers already sent by (output started at …/header.php:5) in …/header.php on line 13 ) Do you think I see this error because I’m testing it on localhost?

596 posts Themes and Graphics
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $250,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+9 more
Bebel says

You probably have some whitespace in front of <?php

Make sure you remove all spaces :)

291 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+3 more
AkinGn says

No, I don’t have any whitespace there. And the code at the line that eror message points is:

setcookie('Color',$Color);

I basically have no idea..

596 posts Themes and Graphics
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $250,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+9 more
Bebel says

Try to put in the very first line <?php session_start() ?>. And I really recommend you to use sessions instead of cookies.

I for example have disabled cookies by default and only activate them on “trusted” sites. Sessions aren’t saved on the users computer, they are stored on the server.

291 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+3 more
AkinGn says

I put the <?php session_start() ?> code at the top of my header.php , it didn’t work. But can you pls paste the code to use sessions to change color? Thanks so much for your replies Bebel :)

1012 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+6 more
wizylabs says

Hi,

Place &lt;?php session_start() ?&gt; at the top of functions.php file then in your header.php use the code below

<?php $_SESSION['color'] = 'blue';
if(isset($_GET['color']))
    $_SESSION['color'] = $_GET['color'];

?>

<link href="<?php bloginfo('template_directory'); ?>/css/<?php echo $_SESSION['color']; ?>.css" rel="stylesheet" media="all" type="text/css" />
291 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+3 more
AkinGn says

I couldn’t get it worked with sessions but I realized that I can make that error disappear by putting taking the cookies method code between <!-- and --> tags lol I got it working somehow with your help, you guys are awesome, thak you very much ! :))

291 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+3 more
AkinGn says

Update: I’ve got session method working!! Thank you soooo much Wizylabs! :)

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by