Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active October 17, 2021 20:55
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 mishterk/d6cafa4fe6b139f03b968634133ee321 to your computer and use it in GitHub Desktop.
Save mishterk/d6cafa4fe6b139f03b968634133ee321 to your computer and use it in GitHub Desktop.
A simple example of using filemtime() to version enqueued assets with their last modified time. Snippets for https://hookturn.io/2021/10/automatic-versioning-strategy-for-enqueued-wordpress-assets/
<?php
// A simple example of using filemtime() to version enqueued assets with their
// last modified time.
add_action( 'wp_enqueue_scripts', function () {
wp_register_style(
'styles',
get_stylesheet_directory_uri() . '/style.css',
[],
filemtime( get_stylesheet_directory() . '/style.css' )
);
wp_register_script(
'scripts',
get_stylesheet_directory_uri() . '/scripts.js',
[],
filemtime( get_stylesheet_directory() . '/scripts.js' )
);
wp_enqueue_style( 'styles' );
wp_enqueue_script( 'scripts' );
} );
<?php
// This example demonstrates how we can use wp_enqueue_style() and wp_enqueue_script()
// functions directly to both register and enqueue the asset in one call.
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_style(
'styles',
get_stylesheet_directory_uri() . '/style.css',
[],
filemtime( get_stylesheet_directory() . '/style.css' )
);
wp_enqueue_script(
'scripts',
get_stylesheet_directory_uri() . '/scripts.js',
[],
filemtime( get_stylesheet_directory() . '/scripts.js' )
);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment