Force Reload of Scripts and Stylesheets in your Plugin or Theme


css-code-smallIf you’re developing a WordPress theme or plugin you may have had the problem that scripts or stylesheets are not reloaded from the source when you refresh the page because they are cached somewhere on the way from the server to the browser. There are various methods to suppress this behaviour like disabling the browser cache in the options or by using a web development add-on. Sometimes this simply does not work because it’s not always apparent where the content is cached since there are so many possibilities and you may have missed to disable all of them.

WordPress provides a simple method to ensure that all stylesheets and scripts are reloaded from the source when they have changed by providing a version parameter:

wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
wp_enqueue_style( $handle, $src, $deps, $ver, $media);

You can increase the $ver parameter every time you’ve changed the files:

wp_enqueue_script( ‘my_script’, ‘my_script.js’, ”, ‘0.11’ );
wp_enqueue_style( ‘my_style’, ‘my_style.css’, ”, ‘0.11’ );

The URL of the stylesheet/script will be changed to ‘.../my_script.js?ver=0.11‘ so that every caching system detects the changed file and reloads it from its source and the user always gets the recent version.

But to change the version number manually every time in the development stage would be a bit tedious and you’re a programmer, right? So let’s automate this:

wp_enqueue_script( ‘my_script’, ‘my_script.js’, ”, time() );
wp_enqueue_style( ‘my_style’, ‘my_style.css’, ”, time() );

The value of time() changes every second so the version ID of the file changes constantly and it’s reloaded from the source and not from some cache.

It’s a bit disadvantageous that you have to substitute the time based version parameter by a “real” version number every time you deploy your code since the actual user still should have the benefits of cached scripts and stylesheets. Let’s extend the idea:

define (‘VERSION’, ‘1.1’);

function version_id() {
if ( WP_DEBUG )
return time();
return VERSION;

wp_enqueue_script( ‘my_script’, ‘my_script.js’, ”, version_id() );
wp_enqueue_style( ‘my_style’, ‘my_style.css’, ”, version_id() );

This way you can make sure that in your development environment everything is reloaded from source all the time but a productive server takes advantage of caches.