WordPress Theme Dev Tip – Dynamic Stylesheets
Friday, September 18th, 2009
A dynamic stylesheet using php is a powerful thing, when accompanied by a user options page it can really make a theme. I’ve recently done this to my Techozoic theme, and I’m going to share how this works and some time saving tips.
First you’ll want some way of letting the theme user choose what will change in the stylesheet, I explain how to do this with a theme options framework here. After you’ve got all the options set the way you want you need some way of getting those into your theme. At first I was just using <style></style> tags in the header of the theme, while this works it just isn’t an efficient way of writing a theme. What I wanted to do was have an external file that changed with the options but that could also be cached by the web browser, to improve performance, save bandwidth and overall have a better written theme.
Now if your using a framework similar to mine or your using mine then you know you have to pull some info from the WordPress database to fill in your variables from your option page. This posed the biggest problem because since this was an external page none of the WordPress functions would work. To get around this in the first version I just loaded the wp-config.php and wp-load.php files in the top of the file. This is bad practice because WordPress site owners can move these files around now and there is now way to know where to load these from. So I spotted the solution from another theme developer. Making an array of all the option values then using php GET variable on the external stylesheet. Now onto the actual code.



