Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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