Last active
November 5, 2021 12:14
-
-
Save MogulChris/654117eb868ad7c218c727b6912e4e6f to your computer and use it in GitHub Desktop.
Automatic versioning with WordPress CSS and JS files (avoid hard refresh problems)
This file contains hidden or 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 | |
//When registering styles and scripts in WordPress, you can specify a 'version' parameter which is appended to the file's URL. | |
//This is fine, but it's inconvenient having to bump the version number whenever you make a change. So why not do it automatically? | |
//Note we use get_stylesheet_directory() instead of get_stylesheet_directory_uri(), so we get an absolute file path for filemtime() | |
$mtime = filemtime(get_stylesheet_directory() .'/js/script.js'); | |
wp_register_script( 'theme-js', get_stylesheet_directory_uri() .'/js/script.js', array( 'jquery' ), $mtime, true ); | |
wp_enqueue_script( 'theme-js' ); | |
$mtime = filemtime(get_stylesheet_directory() . '/css/icon-styles.css'); | |
wp_register_style( 'icons', get_stylesheet_directory_uri() . '/css/icon-styles.css', array(), $mtime ); | |
wp_enqueue_style( 'icons' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment