Skip to content

Instantly share code, notes, and snippets.

@Mte90
Created April 30, 2020 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mte90/f54dd4d47571d54bc3ae76690020f14b to your computer and use it in GitHub Desktop.
Save Mte90/f54dd4d47571d54bc3ae76690020f14b to your computer and use it in GitHub Desktop.
W3TC generate minified filename by checksum
<?php
// Based on https://wordpress.org/support/topic/how-to-change-minified-css-file-name/
// Get all the files, read them, concat them, get a md5 hash, cut at 10 and generate a new filename
// Why?
// W3TC generate the hashname based on the filenames so not invalidate the cache if there are changes
add_filter('w3tc_minify_urls_for_minification_to_minify_filename', 'w3tc_filename_filter', 20, 3);
function w3tc_filename_filter($minify_filename, $files, $type ){
$path_parts = pathinfo( $minify_filename );
$content = '';
foreach ( $files as &$asset ) {
$content .= file_get_contents( ABSPATH . '/' . $asset );
}
return substr( md5( $content ), 10 ) . '.' . $path_parts[ 'extension' ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment