Building a WordPress options panel. One of the things it does is allow users to pick custom colors. I use a default stylesheet, then call styles for custom inputs. However, I simply insert this in the header instead of having these custom values reside in their own stylesheet (since I need to call the user’s choice via PHP).
As an example, I have code in my header like this:
<style type="text/css">
p a { <?php echo get_option('to_custom_css'); ?> }
</style>
And in my functions:
array( "name" => "Custom CSS",
"desc" => "Want to add any custom CSS code? Put in here, and the rest is taken care of. This overrides any other stylesheets. eg: a.button{color:green}",
"id" => $shortname."_custom_css",
"type" => "text",
"std" => ""),
How would I have this reside in its own stylesheet while still using <?php echo get_option('to_custom_css'); ?>
to call the users input?
You can create a stylesheet in PHP, but need to set the Content-type header to text/css. In your HTML:
The in user-styles.php:
First, add this to your
.htaccess
file so that it will interpret php found in css files:Then, link to the external stylesheet. Make the link dynamically include the information needed to determine the user’s css values (probably an id number), like this:
Finally, put php code in the stylesheet that prints out the css dynamically, like this:
Use
$_GET['parametername']
to retrieve the parameters allowing you to calculate the css data.Ultimately you should be writing out the CSS to a file that can be included from the hosting page. That way it can be cached by the browser.