I have some PHP variables that each represent some user settings set via a theme options interface such as:
All these variables are in a file called dynamic-style.php
:
<?php
<style type="text/css">
body {
color: <?php echo $text_color; ?>;
}
</style>
?>
Is there a way to get the parsed CSS code from dynamic-style.php
and save it to the main style.css
at the bottom of the file? I need this to be done to decrease the number of HTTP requests on a public theme.
Thank you.
I have some PHP variables that each represent some user settings set via a theme options interface such as:
All these variables are in a file called dynamic-style.php
:
<?php
<style type="text/css">
body {
color: <?php echo $text_color; ?>;
}
</style>
?>
Is there a way to get the parsed CSS code from dynamic-style.php
and save it to the main style.css
at the bottom of the file? I need this to be done to decrease the number of HTTP requests on a public theme.
Thank you.
The PHP Manual, specifically file_put_contents will point you in the right direction. My suggestion is to write the name/value pairs to an array, insert a comment at the top of a blank CSS file and add an import directive to the master style sheet until you test the output, then dump the array into the test stylesheet.
This isn't exactly the answer you're looking for, as it does not add styles directly to the main stylesheet, but the way this would typically be handled is with the wp_add_inline_style()
function. It would use your primary stylesheet (or whichever enqueued stylesheet you define) as a dependency, but would print the styles inline in the <head>
(rather than in the stylesheet itself), so it at least isn't a call to an additional stylesheet file.