CSS Choice

Permalink 1 user found helpful
Is there a way for the user to be able to choose a css like below?

For example, letting them choose between blue or pink. It's not a theme switcher and this option would only be accessible by the admin.

<link rel="stylesheet" type="text/css" title="blue"
href="http://example.com/css/blue.css">
<link rel="alternate stylesheet" type="text/css" title="pink"
href="http://example.com/css/pink.css">

camnio
 
beebs93 replied on at Permalink Reply
beebs93
You could make a new select option page attribute and simply list the different stylesheets available. Then, in SITE_ROOT/themes/yourtheme/elements/header.php you could do something like:

<?php $strCssFile = $c->getAttribute('select_option_handle_name_here'); ?>

<link rel="stylesheet" media="screen, projection" type="text/css" href="<?php echo $this->getStyleSheet($strCssFile)?>" />
beebs93 replied on at Permalink Reply
beebs93
A quick hacky way to let only the admin/super user have access to change this:

Make a separate page and deny everyone access to read/write/edit etc. Record its collection ID number and modify the code I posted earlier:

$c->getAttribute...

becomes:

Page::getByID(##)->getAttribute...

where ## is the collection ID of said page. Now, only the admin can edit the page (thus change its page attributes).

The obvious downside is that anyone else with access to edit pages will see that page attribute available, however, unless they modify it on that specific page you created it won't have any effect.

Like I said, hacky, but best I could come up with in 5 minutes :)
jordanlev replied on at Permalink Reply
jordanlev
I think this is the way to go. Just to clarify, you create that attribute by going to Dashboard -> Pages and Themes -> Attributes (little tab at the top), then pick "Select" from the dropdown list and click "Add". Fill out the info and add the various options there and then save, then continue with bbeebe's instructions above.
jordanlev replied on at Permalink Reply
jordanlev
Ugh, forum threading not making this clear -- what I meant is that bbeebe's FIRST response is the way to go, and my instructions pertained to that. I don't think his second response makes sense for your particular situation (as he says, it's super hacky, and I think the first response will solve your problem just fine).
beebs93 replied on at Permalink Reply
beebs93
You'll also want to have a default stylesheet ready in case you didn't select any for a particular page.

It's just a matter of adding a simple logic check to my first post:

<?php
$strCssFile = $c->getAttribute('select_option_handle_name_here');
if(strlen($strCssFile) == 0) $strCssFile = 'default.css';
?>
<link rel="stylesheet" media="screen, projection" type="text/css" href="<?php echo $this->getStyleSheet($strCssFile); ?>" />