However, no matter whether you’re inserting the files directly, or enqueuing them, one issue you’ll face is that once you change the file, caching can mean that the old version of the CSS/JS continues to get loaded unless you remember to change the version number of the file.
If you don’t supply a version parameter to either wp_enqueue_style() or wp_enqueue_script(), WordPress will default to enqueuing the file with a version number equivalent to WordPress’ version number which probably makes no sense. Your changes to these files are unlikely to be in sync with updates to WordPress, so either they’ll change unnecessarily when WordPress updates or they’ll not change when you need them to.
What you really want is to change the version number when you update the file. And one thing that we know will be unique each time you edit these files is *drumroll please*… the time.
Yes, every time you save your file the time is different from the last time you did it. So, I’ve started enqueuing my scripts and styles based on the time the file was last modified.
PHP has a handy function for this purpose: filemtime(). It returns the UNIX timestamp for the time the file was last modified.
I used to use YYYYMMDD as my version number for enqueued files, which was reasonably good, but caused issues when it changed more than once in a day and still meant having to remember to update the version number when changes were made to the file. An example of an enqueue might have looked like this:
My revised approach starts by creating a variable for the path of the CSS/JS file and then using filemtime in the version number instead of YYYYMMDD:
Now, instead of my enqueued files containing the WordPress version number, like this:
They look like this:
And I need never worry about the right version of the file showing up again.