Skip to content

Instantly share code, notes, and snippets.

@jonnymaceachern
Last active May 19, 2016 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnymaceachern/4ec400786099569a17755d291be332cb to your computer and use it in GitHub Desktop.
Save jonnymaceachern/4ec400786099569a17755d291be332cb to your computer and use it in GitHub Desktop.
Automatically cache bust scripts and styles
<?php
// wp_register_script( $handle, $src, $dependencies, $version, $includeInFooter )
wp_register_script( 'my-script', get_stylesheet_directory_uri() . '/js/scripts.min.js', array( 'jquery' ), filemtime( get_stylesheet_directory() . '/js/scripts.min.js' ), true );

Notes:

  • The version parameter is appended as a query string ( <script src="http://example.com/script.js?ver=1.0.0">)
  • Appending a query string to a url will force a cache bust, telling the client to re-download the asset
  • Using php's filemtime(), you can get a files last modified time as a unix timestamp and use that as the version number
  • The result is that it will only cache bust when the file is modified
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment