WordPress Code Snippets

A fresh mix of code snippets and tutorials

Combine & compress CSS files with PHP

78

Here is another technique for combining multiple CSS stylesheets into a single, compressed file.

In your CSS directory, create a blank PHP file named “css.php” and add the following code:

<?php // compress and merge CSS files
header('Content-type: text/css');
ob_start("compress");

function compress($buffer) {
	$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
	$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
	return $buffer;
}

include('reset.css');
include('base.css');
include('grid.css');
include('print.css');

ob_end_flush();
?>

Then upload to your server and link to it in the <head> of your web pages:

<link rel='stylesheet' href='http://example.com/css/css.php' type='text/css' media='all'>

Done :)


Show Support

Like our new Facebook Page to show support!