Skip to content

Instantly share code, notes, and snippets.

@BruceMcKinnon
Created March 2, 2025 22:07
Show Gist options
  • Save BruceMcKinnon/f2870fda5cbcc9cb6728cdd364dc6d0f to your computer and use it in GitHub Desktop.
Save BruceMcKinnon/f2870fda5cbcc9cb6728cdd364dc6d0f to your computer and use it in GitHub Desktop.
Enqueue CSS & JS with unique version numbers
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
// Grab CSS and JS urls.
$theme_styles = "/css/child-theme.css";
$theme_scripts = "/js/child-theme.js";
// Use filetime() as the version number - so each time you rebuild the CSS or JS, you get a new version number to bust the browser cache
// Note: we use the local path with filetime(), not a url
$css_vers = filemtime( get_stylesheet_directory() . $theme_styles );
$js_vers = filemtime( get_stylesheet_directory() . $theme_scripts );
// Enqueue the CSS and JS with the version number parameter
wp_enqueue_style( 'child-styles', get_stylesheet_directory_uri() . $theme_styles, array(), $css_vers );
wp_enqueue_script( 'child-scripts', get_stylesheet_directory_uri() . $theme_scripts, array(), $js_vers, true );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment