// Docs
https://developer.wordpress.org/reference/functions/wp_register_script/
// Usage
wp_register_script( $handle, $src, $deps, $ver, $in_footer )
// Docs
$ver (string|bool|null) (Optional) String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added. Default value: false
Q: What happens if I pass an empty string '' to $ver?
Q: If I use a date-time as the value for $ver woll the cache be busted and will the script reload each time the page is refreshed?
How about using dynamic date-time values that change each time the page is loaded? Is this a sound practice?
<?php
$ver = filemtime( get_stylesheet_directory() . '/js/example.js');
// Bust the cache each time page is loaded
add_action( 'wp_enqueue_scripts', 'cache-buster', 15 );
function cache-buster() {
wp_register_script( 'cache-buster', get_stylesheet_directory_uri() . '/js/example.js', array(), $ver, true );
wp_enqueue_script( 'cache-buster' );
}
Q: If $ver is false because the version of WordPress remains a static value until WordPress is updated will the script remain cached because it has the same value?
Q: If $ver is null is the script cached or not?
What are the best practices and optimal values for registering scripts in WordPress?