1

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 } ?>

2 Answers 2

0

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.

2
  • Thanks for this, maybe I'm having a slow day, but how would I implement this only if someone chose the "left" option? Oct 27, 2011 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. Oct 27, 2011 at 13:54
0

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.

1
  • Unfortunately this needs to be hardcoded as it is for a theme that is going on general release Oct 27, 2011 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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