7

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.

3
  • why not using inline styles? it's faster than making another WordPress call just to process your stylesheet Commented Jan 15, 2011 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
    Commented Jan 15, 2011 at 21:04
  • For anyone ending up here after so many years: use the theme customizer API...
    – cjbj
    Commented May 22, 2016 at 12:50

1 Answer 1

4

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.

3
  • 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.
    – Anh Tran
    Commented Jan 16, 2011 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
    Commented Jan 16, 2011 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 Commented Jan 18, 2011 at 14:20

Your Answer

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

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