Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I display a different style snippet if a user selects and option from my theme options page select box.

For example. To show the sidebar on the right a user would select "Right" and the default stylesheet would be used. However, if they chose "Left" to display it on the left, a small style snippet would appear that overrides the default stylesheet.

This is how I do it for checkboxes if that helps.

<?php if ( of_get_option('show_post_sidebar', false ) ) { ?>
        <!-- default styles would be used -->
    <?php } else {?>
        <style type="text/css" media="screen">
            .post{width:720px}
        </style>
    <?php } ?>
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Just put your dynamic CSS inside of a callback function, hooked into wp_print_script, e.g.

function mytheme_custom_css() {
    ?>
    <script type="text/css">
    <?php if ( of_get_option('show_post_sidebar', true ) ) { ?>
        .post{width:720px}
    <?php } ?>
    </script>
    <?php
}
add_action( 'wp_print_scripts', 'mytheme_custom_css' );

Add as few, or as many, conditional CSS rules as you need.

share|improve this answer
    
Thanks for this, maybe I'm having a slow day, but how would I implement this only if someone chose the "left" option? –  Dean Elliott Oct 27 '11 at 13:27
    
Just change if ( of_get_option('show_post_sidebar', true ) ) to whatever, as appropriate. Without knowing your options structure, I can't answer any more precisely. –  Chip Bennett Oct 27 '11 at 13:54
add comment

I myself have been researching this for my personal blog. There seem to be a few plugins if you're not opposed to using one.

Edit: WordPress appears to have a built-in function for this.

Edit again: I'm sorry but I think I mis-read your question. I assumed that you were looking for a way to change themes based on user preference. I guess I should have read more carefully rather than skimming quickly.

share|improve this answer
    
Unfortunately this needs to be hardcoded as it is for a theme that is going on general release –  Dean Elliott Oct 27 '11 at 13:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.