Skip to content

Instantly share code, notes, and snippets.

@petskratt
Last active September 22, 2018 14:32
Show Gist options
  • Save petskratt/3acd7166297ff4451a606115b49ad22c to your computer and use it in GitHub Desktop.
Save petskratt/3acd7166297ff4451a606115b49ad22c to your computer and use it in GitHub Desktop.
<?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