UPDATE: The colour-changing style sheets men­tioned here are no longer in use, but the code below should still serve as a good example.

You might have noticed lately that the colour-scheme of this site changes each time you click a link or reload a page (cur­rently we have blue, pink, and green). This is some­thing eas­ily accom­plished with just an extra 2 tiny CSS files, and a little bit of PHP.

If you’ve read my pre­vi­ous art­icle — Word­Press And A Hori­zontal Baseline Pt.1 — you are already famil­iar with the CSS that forms the found­a­tion of this site. What we’re going to do first is strip out any­thing to do with col­our from the CSS and put it in it’s own sep­ar­ate file that we’re going to call blue.css.

a:active, a:hover, .commentlist a:hover, .commentlist a:active, .commentlist cite a:hover, .commentlist cite a:active, #recent-comments a:hover, #related-posts a:hover, .lastfm a:hover, #footer a:hover, #footer a:active, .commentlist pre a:hover, .commentlist pre a:active {color:#34B0F8;} 

#menu ul li a:hover, #menu ul li a:active {background:#34B0F8;}

.entry p a:hover img, .entry p a:active img {border:1px solid #34B0F8;}

a.feature:hover	{border-color:#34B0F8;}

Then, after a little mess­ing around with the col­our tool in Pho­toshop, we know the hex code to edit in for pink.css.

a:active, a:hover, .commentlist a:hover, .commentlist a:active, .commentlist cite a:hover, .commentlist cite a:active, #recent-comments a:hover, #related-posts a:hover, .lastfm a:hover, #footer a:hover, #footer a:active, .commentlist pre a:hover, .commentlist pre a:active {color:#F834E3;}
#menu ul li a:hover, #menu ul li a:active {background:#F834E3;}

.entry p a:hover img, .entry p a:active img {border:1px solid #F834E3;}

a.feature:hover	{border-color:#F834E3;}

As well as green.css.

a:active, a:hover, .commentlist a:hover, .commentlist a:active, .commentlist cite a:hover, .commentlist cite a:active, #recent-comments a:hover, #related-posts a:hover, .lastfm a:hover, #footer a:hover, #footer a:active, .commentlist pre a:hover, .commentlist pre a:active {color:#46F834;}
#menu ul li a:hover, #menu ul li a:active {background:#46F834;}

.entry p a:hover img, .entry p a:active img {border:1px solid #46F834;}

a.feature:hover	{border-color:#46F834;}

The next part is to write the PHP, which is just a simple ran­dom func­tion fol­lowed by an if-else state­ment. The code must go between the <head> and </head> tags in your header.php file. It’s also a good idea to make sure that it is after you call your main CSS (style.css) file.

<?php
$random = rand(1,3);
if ($random == 1) {
    echo "<link rel='stylesheet' href='http://yourwebsite.com/wp-content/themes/yourtheme/css/pink.css' type='text/css' media='screen' />";
} elseif ($random == 2) {
    echo "<link rel='stylesheet' href='http://yourwebsite.com/wp-content/themes/yourtheme/css/green.css' type='text/css' media='screen' />";
} else {
    echo "";
}
?>

As the main CSS file already provides the styles for the col­our blue, there is no need to call it again in our PHP func­tion. Now every time that some­body vis­its the site there is a ⅔ chance that they will get a dif­fer­ent col­our! To add any more col­ours you will need to change the 3 in $random = rand(1,3); to a lar­ger num­ber, and add more elseif statements.


Leave a Reply

Details

'Random Stylesheets in WordPress' was posted on June 29th, 2009 in the Category: Tutorials.

You can subscribe to the comments on this post, or post a comment of your own



Related Posts