0

I have some PHP variables that each represent some user settings set via a theme options interface such as:

  • theme colors
  • typography
  • etc.

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.

2 Answers 2

0

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.

2
  • @E Carter Young thanks but I do not think it is a good idea to use file_put_contents in a theme development since the theme check plugin output some warnings regarding to it. Also if I'm not mistaken, some servers do not allow file_put_contents().
    – Knott
    Commented May 29, 2014 at 15:36
  • If it isn't allowed, even for testing, use the PHPFile Methods instead. Read From Files to Upload in the tutorial for all the functions. These are Manual: Wrapper Functions for the function I gave you. If these are acceptable I'll update my answer.
    – eyoung100
    Commented May 29, 2014 at 15:51
0

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.

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.