Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: Zone Cachebuster
Plugin URI: https://gist.github.com/petskratt/3acd7166297ff4451a606115b49ad22c
Description: Replace JS and CSS version parameters with hash - for obfuscation or quick cachebusting.
Author: Peeter Marvet
Version: 0.1
Author URI: https://www.zone.ee/
*/
function zone_hash_ver_css_js( $src ) {
if ( strpos( $src, 'ver=' ) !== false ) {
$parts = parse_url( $src );
$query = array();
parse_str( $parts['query'], $query );
$version_hash = substr( hash_hmac( 'md5', $query['ver'], defined( 'AUTH_KEY' ) ? AUTH_KEY : '' ), 0, 6 );
$src = add_query_arg( 'ver', $version_hash, $src );
}
return $src;
}
add_filter( 'style_loader_src', 'zone_hash_ver_css_js', 9999 );
add_filter( 'script_loader_src', 'zone_hash_ver_css_js', 9999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment