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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0