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:

<style type="text/css">
body {
    color: <?php echo $text_color; ?>;

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.

  • @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 May 29 '14 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 May 29 '14 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.