0

// 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?

1 Answer 1

1

Here are the answers to your questions -

  1. If you pass '' to $var it will consider as false and the default WordPress version number will be added as a $var to query string.

  2. If you use date-time value then yes, the script will be loaded each time on page refresh. So it will solve versioning issue. But it can cause page load time issue because we are not caching the script files and every time they are loading as a new.So, I think it should not be the good practice.

  3. Yes the script remains cached because it has same value.

  4. Yes, the script will get cached.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.