WordPress makes enqueuing assets super simple and generally does its very best to keep those assets up to date where possible but we can take this a step further and use PHP’s filemtime()
function to ensure our JavaScript and CSS files are given a new version whenever we make changes to them.
An example of automatic versioning when using wp_register_style()
and wp_register_script()
By simply passing the asset’s full file path to filemtime()
and using that in the $version
parameter of either wp_register_style()
or wp_register_script()
, we can let PHP & WordPress manage the file’s version number and update the URI as we make changes to the files.
Note: wp_enqueue_style()
and wp_enqueue_script()
can also register assets for us directly
It is possible to skip the use of the wp_register_style()
and wp_register_script()
functions in the previous example and instead pass the additional arguments directly to the enqueue functions. Under the hood, the enqueue functions check for additional arguments and found, will register the asset before enqueuing.