Created
August 12, 2014 04:29
-
-
Save tillkruss/8c71b24165b18db97439 to your computer and use it in GitHub Desktop.
add query string cache buster to stylesheets minified by W3 Total Cache
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 | |
// add query string cache buster to W3TC minified stylesheet links | |
add_action( 'init', function() { | |
// is css minify enabled? | |
if ( isset( $GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify' ] ) && $GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify' ][0]->_config->get_cache_option( 'minify.css.enable' ) ) { | |
// store original minify callback | |
$GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify-org' ] = $GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify' ]; | |
// replace minify callback | |
$GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify' ] = function( &$buffer ) { | |
// run original minify callback | |
$buffer = call_user_func( $GLOBALS[ '_w3tc_ob_callbacks' ][ 'minify-org' ], $buffer ); | |
// add `?ver=<your-cache-buster>` cache buster to stylesheet link | |
$buffer = preg_replace( '~<link(.+)href="(.+\/cache\/minify\/.+)"(.+)\/>~U', '<link$1href="$2?ver=<your-cache-buster>"$3/>', $buffer ); | |
return $buffer; | |
}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment