How to save the output of php variables used in inline css code to the main style.css?

admin2025-01-07  5

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.

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.

Share Improve this question edited May 29, 2014 at 15:24 eyoung100 2831 silver badge11 bronze badges asked May 29, 2014 at 15:11 KnottKnott 4542 gold badges7 silver badges27 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254259a190.html

最新回复(0)