Skip to content

Instantly share code, notes, and snippets.

@mrsize
Last active January 27, 2020 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrsize/002f7d7022d7d3bd6fd1b2beef39d141 to your computer and use it in GitHub Desktop.
Save mrsize/002f7d7022d7d3bd6fd1b2beef39d141 to your computer and use it in GitHub Desktop.
Wordpress Minify HTML for visitors
/*
** Minifier le HTML :
*/
// Enable GZIP output compression
if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler"))
add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);'));
// ob_start = mise en tampon
add_action('get_header', 'mytheme_html_minify_start');
function mytheme_html_minify_start() {
if( !isset($_GET['min']) ){
ob_start( 'mytheme_html_minyfy_finish' );
}
}
// Minifier les fichiers HTML d'un site WordPress sans plugin
if (!current_user_can('update_plugins')) {
function mytheme_html_minyfy_finish( $html ) {
// Suppression des commentaires HTML,
// sauf les commentaires conditionnels pour IE
$html = preg_replace('/<!--(?!s*(?:[if [^]]+]|!|&gt;))(?:(?!-->).)*-->/s', '', $html);
// Suppression des espaces vides
$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