
Ever wanted to allow your members to each choose from one of multiple colour schemes or backgrounds? This tutorial will teach you how to add a jQuery powered stylesheet switcher to your phpBB forum.
What we’re trying to achieve
User customisation is becoming more and more popular in forum software. Things like collapsible categories, collapsible sidebars, customisable backgrounds, choice of colour scheme are all things that nobody could really imagine would have existed in styles 5 years ago. Now they play a huge part in separating the best styles from the rest. This tutorial will teach you how to add a jQuery stylesheet switcher to your forum, allowing your users to choose between different colour schemes.
Preparation
You’ll need a decent Source code editor to complete this tutorial. You should also download the stylesheet-switcher.zip archive and then:
Copy: /stylesheet-switcher/jquery.cookie.js to /styles/prosilver/template/
Copy: /stylesheet-switcher/blank.css to /styles/prosilver/theme/
Copy: /stylesheet-switcher/colours to /styles/prosilver/theme/
Step 1) HTML Edits
Firstly we need to connect to jQuery and the cookie plugin. Next we’ll be adding the blank stylesheet connection (used to override the default style’s style information). Finally all we need to do here is add the links that the end user will use to change between the stylesheets.
Open: /styles/prosilver/template/overall_header.html
Find:
Below, Add:
<script type="text/javascript" src="{T_TEMPLATE_PATH}/jquery.cookie.js"></script>
Find:
<link href="{T_THEME_PATH}/bidi.css" rel="stylesheet" type="text/css" media="screen, projection" />
<!-- ENDIF -->
After, Add:
Find:
After, Add:
<div class="navbar">
<div class="inner"><span class="corners-top"><span></span></span>
<ul class="linklist rightside">
<li><strong>Colour:</strong></li>
<li><a href="#" rel="{T_THEME_PATH}/colours/red.css" class="switch">Red</a> • </li>
<li><a href="#" rel="{T_THEME_PATH}/colours/green.css" class="switch">Green</a> • </li>
<li><a href="#" rel="{T_THEME_PATH}/colours/yellow.css" class="switch">Yellow</a> • </li>
<li><a href="#" rel="{T_STYLESHEET_LINK}" class="switch">Default</a></li>
</ul>
<span class="corners-bottom"><span></span></span></div>
</div>
Pay attention to the rel=”" attribute of each <a> tag. The stylesheet linked here will populate a blank stylesheet called after phpBB’s, therefore overwriting the default style information. We also add a “default” link which links to the default style’s CSS. It will essentially define the same stylesheet twice, but is a lot more graceful than writing a blank cookie over the user’s colour choice and then forcibly refreshing the page.
Step 2) CSS Edits
This step is largely down to yourselves and what you want to do with the script. Those of you well versed in CSS will know exactly how to override CSS information, if you’re not familiar with CSS then this probably isn’t for you.
Here we are going to take a really basic look at green.css:
This is called after phpBB's stylesheet, so !important shouldn't be required in most cases */
html, body {
background-color: #009900;
}
When this stylesheet is activated, the colour #009900 will override prosilver’s default white. Instead of using colours it is possible to add any style information to change element. For example, you could override the forabg and forumbg to re-colour the category / forum borders. You could override .headerbar to change the blue header, or even just give your users a choice of different background images instead of colours.
Step 3) The jQuery
This is where all the magic happens. It should be straight forward to understand reading through the code.
Open: /styles/prosilver/template/overall_header.html
Find:
Below, Add:
if($.cookie("colour-choice")) {
$("link.colour-switcher").attr("href",$.cookie("colour-choice"));
}
$(document).ready(function() {
$("a.switch").click(function() {
$("link.colour-switcher").attr("href",$(this).attr('rel'));
$.cookie("colour-choice",$(this).attr('rel'), {expires: 365, path: '/'});
return false;
});
});
</script>
Assuming you’ve done everything correctly, you should now be able to change between colours on your style by clicking the different coloured links.
Finishing off
It’s important to know that if you’re an Administrator, your chosen colour will not carry over into the “password confirmation screen” when trying to log into the Administration Control Panel. This is because the cookie the script writes your preference to is in one directory, whereas the ACP (and authentication) is in another directory entirely.
If you found this tutorial useful, please consider donating. If you have any serious problems, let me know in the comments below. Enjoy.




22 Responses
Hi.
It is possible to apply this switcher to the Absolution style and make it look like http://demo.phpbb3styles.net/Supernova ??
And AFAIK the zip archive missing switch.css
Hi, the file copy should have said blank.css, not switch.css – I have fixed that.
As for applying it to Absolution that should be quite easy. You can use the colour icons from supernova, so rather than:
[xml]<a href="#" rel="{T_THEME_PATH}/colours/red.css" class="switch" rel="nofollow">Red</a>[/xml]
You could use:
[xml]<a href="#" rel="{T_THEME_PATH}/colours/red.css" class="switch" rel="nofollow"><img src="{T_THEME_PATH}/images/red-icon.png" width="16" height="16" alt="" /></a>[/xml]
Christian
Thx !
Thank you very much, driving me was very helpful. Now you can not make a guide to move the category? Just as styles in the supernova?
That will be the next tutorial I write
. Don’t ask for a time though, bit busy at the moment.
Ok well I’ll wait. Thanks
Hello, I noticed one thing, I still use an old version of firefox and when I change color and also changing the whole page is ok. But rather those who use the new version of firefox changing color and returns the page’s default color. The same goes with other browsers, what happened?I have done something wrong?
You’ll need a decent Source code editor editor
—
A small mistake, you typed editor twice
btw, nice tutorial.
Awesome code Christian, like always. Keep up the good work. Can’t wait to see ForaHQ live!
Why when i refresh site style back to deflaut :/
What’s the link to your site?
Hmm, style switching works fine, but it doesn’t save the chosen color. Refreshing or going to another page returns the default style.
love the theme. looks clean and easier on the eyes. By default you have red, green & blue to chose from.
have you posted other colors (ie: purple or black) that a non programmer like myself can download and install?
Love the color switcher, but it seems to have some problems with the latest Opera browser. It shows the colored squares about 5px higher than they should be, and the hover effect doesn’t really work.
Hi
i use this system in my forum but cookie not working exactly.
when i refreshed the page the default css come again.
i put the
and
if($.cookie(“colour-choice”)) {
$(“link.colour-switcher”).attr(“href”,$.cookie(“colour-choice”));
}
$(document).ready(function() {
$(“a.switch”).click(function() {
$(“link.colour-switcher”).attr(“href”,$(this).attr(‘rel’));
$.cookie(“colour-choice”,$(this).attr(‘rel’), {expires: 365, path: ‘/’});
return false;
});
});
in header but not working yet. plz help me.
my forum is
http://www.zohall.com/tinyzo
Hey Cristian,
would it be possible to have images in background rather then colors? is that doable?
This is great Christian, thanks!
One question: How come for the default choice you have it link to {T_STYLESHEET_LINK} ? Why not just link it back to blank.css?
Otherwise, doesn’t your method wind up loading the {T_STYLESHEET_LINK} css file twice? Because that css file is already linked to by phpBB anyway, and then your code has it loaded/linked to a second time.
Well written tutorial. Crisp as usual..
Hi! Can i use your stylesheet switcher in custom themes ? I mean sell themes with stylesheet switcher included.
Thank’s in advance !
Hi George. That’s fine, would appreciate either a footer mention or mention in the readme file if you choose to include it. Sharing the love and all that.
I made custom jQuery library file to make lesser load on server
BTW I love your customisations <3