Created
April 30, 2020 16:50
-
-
Save Mte90/f54dd4d47571d54bc3ae76690020f14b to your computer and use it in GitHub Desktop.
W3TC generate minified filename by checksum
This file contains hidden or 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
<?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