Last active
February 18, 2020 09:35
-
-
Save nikola0203/dfa292e4e497419e337db8080fb2cf78 to your computer and use it in GitHub Desktop.
Minify CSS with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get CSS | |
$css = get_option( 'css' ); | |
// Remove comments | |
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css); | |
// Remove space after colons | |
$buffer = str_replace(': ', ':', $buffer); | |
// Remove whitespace | |
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); | |
// Display CSS | |
if ( $buffer ) { | |
?> | |
<style type="text/css"><?php echo $buffer; ?></style> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment