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.

I would like to create a theme options page based only on changing the site wide CSS styling. For instance body/container background colors, font-sizes, font colors, etc.

I have accomplished this by using a stylesheet link to a php file in the header that echo's the CSS and uses get_option.

For instance background-color: <?php echo get_option('background_color'); ?>;

Would I be correct in assuming this is a bad idea performance wise if there are many options?

I have tried other methods but they all seem to add inline or embedded styles, which I do not want, how would one get around this?

Would creating a custom script that writes to the static CSS file be a good idea?

Is there any way the settings API can handle this?

** PS. Great answer but I have decided to actually go with writing to a static file as it provides way less overhead.

share|improve this question
    
why not using inline styles? it's faster than making another WordPress call just to process your stylesheet –  onetrickpony Jan 15 '11 at 20:52
    
I would like to try an avoid inline or embedded styles as good practice, I dislike coming across it and prefer the separation of content and design, it adds maintenance problems, accessibility and other issues, so ya I want to know if there are other options. –  Wyck Jan 15 '11 at 21:04

1 Answer 1

up vote 3 down vote accepted

creating a custom script that writes to the static CSS file is a bad idea!!! you would need to regenerate the file each time you save any changes.

a better solution would be to save all options in an array of options say for example:

$MyCSS['background_color'] = #009988;
$MyCSS['background_repeat'] = no-repeat;
update_option('theme_settings',$MyCSS);

and that why you only call the "get_option()" function once.

    $MyCSS = get_option('theme_settings');
// and use :
    echo $MyCSS['background_color'];

and make much less calls to the database and use less resources if you are thinking performance wise.

share|improve this answer
    
Your solution is the same as Wyck did. But I think using static CSS file is better. Because you don't usually update this file, and update happens only once when you update all options. In other time, your PHP script doesn't have to do anything (even echo the values), and CSS file is send statically to user, which can be cached. –  Rilwis Jan 16 '11 at 10:23
1  
my solution is a bit different in witch it makes much less calls to get_option function and yes using static CSS file is always better but less customizable. –  Bainternet Jan 16 '11 at 11:02
    
Yeah, I noticed too a 20-30% decrease of db queries when calling get_option only once per option. I usually make a get_option wrapper function which stores retrieved options in a global array that can be accessed if the same option is requested later. WP should have this built in –  onetrickpony Jan 18 '11 at 14:20

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.