I had tried for a while to find a way to allow users to choose their favorite theme for this website. I decided to do it as a Wordpress plugin, which I offer today for other Wordpress users. Installing it is quite simple: Download, rename to stylesheet_chooser.php, upload it to your wp-contents/plugins directory and activate it from the admin interface.
To define which stylesheets to use (no, it's not automatic...), you have to define an array called $styles, before you call any of the other functions.You can do it like this, for example : $styles=array("styles/new","styles/default","styles/no-style"); if your stylesheets are in the styles directory. Omit the '.css' part. The first one in the list will be used as your default style. The plugin will automatically selectthe last part of the path (in this case new, default and no-style) and use them as the text in the selection box.
In the plugin file (stylesheet_chooser.php), change the two lines below to use text in your language:
<span class="warning"><?php _e("Si votre navigateur en était capable, vous pourriez ")?></span>
<label for="newstyle"><?php e('Change style...')?></label>
In your template (index.php) you have to insert two functions:
style_link() will insert the style sheets link. You have to put it between the <head> and </head> tags in your template.
The optional parameter - for example style_link(false) - can be set to 'true' (default) if you want to @import the style sheets (ignored by NS4), or to 'false' to get this kind of links: <link rel="stylesheet" title="....> for the current chosen style sheet and like this: <link rel="alternate stylesheet" title="....> for the other(s).
style_chooser() will insert the form that is used to select the stylesheet. Place it wherever you want, in your sidebar for example.
In version 0.23+, it is possible to have a short comment appear below the selection dropdown. For this, the only thing you have to do is insert it in your stylesheets like this:
/*
Comment: Write your comment here.
*/
And that's it, now each stylesheet has it's own "introduction text".
Now that you know all this, you can :
- Download,
- write a comment,
- or Contact me in case you have problems

28 comments about 'Stylesheet Chooser Version 0.2'
Get the comments feed for this entry or post a trackback from your own blog
The style chooser only works when you have cookies enabled. I always have cookies disabled in Firefox, with exceptions for a couple of sites. I don't know if there is a workaround possible so this works without the use of cookies, but that might be preferable to this solution if it works in all situations.
Dear Jonathan, why does the plugin created this stylesheet-tag at the end <link rel="alternate stylesheet"type="text/css" media="screen" title="" href="http://www.jfoucher.marinetechs.com/css"
It is possible to use sessions instead of cookies, but I have trouble understanding their behaviour. I will try to find a way, though. Keep in touch if you're interested in the cookie-free version.
That was a little bug, thanks for pointing it out.
It's fixed in the new version.
show_chooser() ?? When you've got style_link(), it'd be more understandable to choose style_chooser() as function name. Makes it more easy to see what functions come from what plugin
Done!
Hi, the download does not work, it is redirected to http://www.jfoucher.marinetechs.com/2004/09/02/stylesheet-chooser/
It should be fixed now. Let me know if you still have problems, ok ?
Sorry to all the recents commenters. I had some database problems and lost some of the newest comments. I even lost one post that wasn't backed up, but got it back from Google's cached version. Thanks !
Thanks to Francesco too, for correcting that bug.
Hi!
I have a problem with this plugin. It displays the default stylesheet but I'm not able to change the stylesheet to another. It just gives me a 404 error page, what's wrong?
And it doesn't insert the alternative stylesheet links (
same problem here.
i've fixed it this way: around line 156 in the plugin you find the form. action is srt to "/", replace it with "< ?php echo $_SERVER["REQUEST_URI"]; ?>"
Yes! of course!
Francesco, a big thank you to you!
Whippee!
Thank you, Jonathan, for this great plugin and thank you, Francesco, for this great fix!
Ok, I managed to restore the comments from the notifications I received by email, but I don't know if you were subscribed to comments. If you were, you aren't anymore, I didn't want to force this on anybody...
now i have another small problem: i have to change style twice in order to see it applied. the weird thing is that on my server at home i just need to switch once, while on my provider's one i have to do it twice. Maybe an issue with different versions of php? i haven't had the time to check the code again to see if i can fix this. anyone else had the same problem?
In fact, using $_SERVER['REQUEST_URI'] didn't work at all for me, so I used get_bloginfo('url'), which gives the correct url in all cases.
Hello,
I'm not very familiar with WordPress or PHP. Where do I have to create the array you mention? ($styles=array("styles/new","styles/default","styles/no-style")) Could you please elaborate a little on this?
Merci beaucoup,
Erwin
Anywhere above the style_link() function call is fine.
If you have to css file named wp-layout.css and wp-layout2.css in the same directory as your index.php file, the array definition would look like this :
Hope this helps.
I'm getting the following error:
Fatal error: Call to undefined function: lp() in /home/margaret/public_html/wp-content/plugins/stylesheet_chooser.php on line 184
You can see it in action. I, too, am not versed in php, so I'm just sort of muddling my way through.
Yeah, sorry about that, my mistake. Should be solved now if you download it again. But I see you got your problem solved. Nice looking alternative style, BTW.
I have a problem similar to the one Antti Leväsaari experienced:
Yet the website shows all three CSS styles but only loads the one listed first. Have I missed a step?
here's the example... http://www.strangeisle.com/
Thank you.