Skip to content

Instantly share code, notes, and snippets.

@Firestorm980
Created September 17, 2015 16:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Firestorm980/e7ad991677d779a08d81 to your computer and use it in GitHub Desktop.
Save Firestorm980/e7ad991677d779a08d81 to your computer and use it in GitHub Desktop.
WordPress cache busting
/*
The trick to this whole thing is the cache busting "filemtime()" function.
It takes the file creation time and makes that the version number.
So, we don't have to keep track of versions and it will always be unique.
*/
// You don't have to define this stuff, but it can be useful for later since we reference them so much.
define( 'THEME_DIR_URI', get_template_directory_uri() );
define( 'THEME_DIR', get_template_directory() );
// Our function. Doesn't have to be named like this.
function scripts_and_styles(){
// JS example
wp_register_script('nameOfScript', THEME_DIR_URI . '/pathtofile/script.js', array(''), filemtime( THEME_DIR . '/pathtofile/script.js' ), true );
wp_enqueue_script('nameOfScript'); // Enqueue it!
// CSS example
wp_register_style('nameOfStyle', THEME_DIR_URI . '/pathtofile/style.css', array(), filemtime( THEME_DIR . '/pathtofile/style.css' ), 'all');
wp_enqueue_style('nameOfStyle'); // Enqueue it!
}
// Add our function to the WP actions
add_action('wp_enqueue_scripts', 'scripts_and_styles'); // Add Theme Scripts and Styles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment