Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mishterk/0473fc07ff9a0c88e4cf8ae9f22a7f75 to your computer and use it in GitHub Desktop.
Save mishterk/0473fc07ff9a0c88e4cf8ae9f22a7f75 to your computer and use it in GitHub Desktop.
How to automatically version WordPress assets using their last modified time. More information at https://philkurth.com.au/tips/how-to-automatically-version-wordpress-assets-using-their-last-modified-time/
<?php
add_action( 'wp_enqueue_scripts', function () {
$uri = get_stylesheet_directory_uri();
$dir = get_stylesheet_directory();
$script_last_updated_at = filemtime( "$dir/scripts/my-script.js" );
$style_last_updated_at = filemtime( "$dir/styles/my-style.css" );
wp_register_script( 'my-script', "$uri/scripts/my-script.js", [], $script_last_updated_at, true );
wp_register_style( 'my-style', "$uri/styles/my-style.css", [], $style_last_updated_at, true );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment