How to enqueu php files with custom variable & conditionals?

Hi I have taken over a website with half-customized theme. It was an outdated theme with bunch of scripts and formatting. Currently I encountered a small issue when enqueue stylesheets in functions.php instead of header.php (since all of them were included directly in the header.php).

Problem #1

Theme has specific ‘style.php’ loaded directly to header.php files which has:

header("Content-type: text/css",true); 

function compress($buffer) {
  /* remove comments */
  $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
  /* remove tabs, spaces, newlines, etc. */
  $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
  return $buffer;

/* css files */
include('css/custom.css'); // ALL CUSTOMIZATION goes here!


and I am wondering whether to load all files separately or somehow enqueue .php file.

Problem #2

It has dynamic option to change colors and thus this is loaded straight to header.php with lines:

<?php $primary_color = $data['primary_color'];?>
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/css/color.php?color=<?php echo substr($primary_color,1) ?>" type="text/css" media="screen" />

How can I move this to functions and use regular wp_enqueue?

Thank a lot!

Solutions Collecting From Web of "How to enqueu php files with custom variable & conditionals?"

From the enqueue point of view it doesn’t matter if it points to genuine static CSS file or endpoint that dynamically generates it.

However there is a massive performance implication. Loading PHP engine (and worse — WordPress core on top) is much much more resource intensive than serving static file.

Your end goal shouldn’t be converting this, but getting rid of this altogether. If that is a standalone site (and not theme meant for redistribution) it should be pretty easy to generate desired modification once and serve them as static files from that point.