Skip to content

Instantly share code, notes, and snippets.

@Vitzkrieg
Created January 19, 2022 17:17
Show Gist options
  • Save Vitzkrieg/4b40e85625050ef864c98c9a7c2f2d9f to your computer and use it in GitHub Desktop.
Save Vitzkrieg/4b40e85625050ef864c98c9a7c2f2d9f to your computer and use it in GitHub Desktop.
<?php
add_action('get_header', 'pt_html_minify_start');
function pt_html_minify_start() {
ob_start( 'pt_html_minyfy_finish' );
}
function pt_html_minyfy_finish( $html ) {
$html = preg_replace('/<!--(?!s*(?:[if [^]]+]|!|>))(?:(?!-->).)*-->/s', '', $html);
$html = preg_replace('/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/m', "$1", $html);
$html = preg_replace('/^\/\/[^\n\r]+(?:[\n\r]|\*\))$/m', "$1", $html);
$html = str_replace(array("\r\n", "\r", "\n", "\t"), '', $html);
while ( stristr($html, ' '))
$html = str_replace(' ', ' ', $html);
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment