Last active
October 17, 2021 20:55
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
} ); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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