Force the browser to fetch a new style.css each time the page is loaded

I'm constantly making new changes and then migrating them to my live site, even minor adjustments!
This can however, be quite bad..

My clients, friends, random internet warlocks.. they all see old CSS, or none at all, for new content and containers.
HTML changes, PHP changes, CSS stays the same!

This is due to the cache time, your browser will use the default expiry time of a file that hasn't changed and with CSS this seems to be used even if changes were made!

 

In a previous post we went through how to add your own CSS file and make a browser fetch it each time, but why not the default style.css?
Well.. It's not good practice to force the user to fetch new stylesheets each time, this is not a long term solution..

 

Check your functions.php, where it references and enqueues your stylesheet we need to change the second to last value (the version) to:

time()

This will add the time as the version, so every second you go up a new version number!
Each time the page is reloaded you're sure to get a new CSS copy without clearing the cache or force refreshing.

Here's that in full.

wp_enqueue_style('freshlondon-style', get_stylesheet_uri(), array(), time(), false);

Enqueueing styles and scripts correctly in WordPress

In the past I've been creating themes while mistakenly adding scripts and stylesheets to the WordPress header.php file. I would now like to add these properly using wonderful built in WordPress functions.

Here's the WordPress default syntax for enqueueing styles and scripts:

https://gist.github.com/FreshLondon/3fc042ae077d35dd838a82a39e1bac3f

 

Parameters: wp_enqueue_script

Let's break these down..

$handle

(string) (Required) Name of the script. Should be unique.

This is most useful if you'll be registering and then enqueueing the script later, or what we'll be using it for: another scripts $deps (dependency).

$src

(string) (Optional) Full URL of the script, or path of the script relative to the WordPress root directory.

Default value: ' '

Specify the location of your script, remember we can use scripts located within the themes folders using built in functions. We'll touch in on this one later below!

$deps

(array) (Optional) An array of registered script handles this script depends on.

Default value: array()

The dependancies, what does this depend on? In short and simple: what should be loaded before this?

$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

The given 'version' is mostly used for caching. A users browser will check if it needs to load new resources for a webpage and if a new version of a script or style has been added it'll fetch this.
Setting to the default value 'false' sets a cached script only to be renewed when the cache expiry time has been reached.
If you'd like to force the browser to ALWAYS load the script each time, use the PHP time() function.

$in_footer

(bool) (Optional) Whether to enqueue the script before </body> instead of in the <head>. Default 'false'.

Default value: false

This one here is self explanatory..

 

 

Parameters: wp_enqueue_style

Let's break these down..

$handle

(string) (Required) Name of the stylesheet. Should be unique.

Again this is most useful if you'll be registering and then enqueueing the style later, or what we'll be using it for: another styles $deps (dependency).

$src

(string) (Optional) Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.

Default value: ' '

Specify the location of your stylesheet, remember we can use stylesheets located within the themes folders using built in functions. We'll touch in on this one later below!

$deps

(array) (Optional) An array of registered stylesheet handles this stylesheet depends on.

Default value: array()

The dependancies, what does this depend on? In short and simple: what should be loaded before this?
Remember: a stylesheet loaded after another will take precedence.

$ver

(string|bool|null) (Optional) String specifying stylesheet 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

The given 'version' is mostly used for caching. A users browser will check if it needs to load new resources for a webpage and if a new version of a script or style has been added it'll fetch this.
Setting to the default value 'false' sets a cached script only to be renewed when the cache expiry time has been reached.
If you'd like to force the browser to ALWAYS load the stylesheet each time, use the PHP time() function.

$media

(string) (Optional) The media for which this stylesheet has been defined. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.

Default value: 'all'

Is this stylesheet to be loaded on all @media types? Do you want to load it for printing, probably not!
A value such as 'screen' will load the stylesheet only for digital screens whereas a value of 'print' will load the stylesheet for printers only.

 

Conclusion

So thats the parameters explained, here's how that looks in play:

https://gist.github.com/FreshLondon/fcc79836d704377e4e67b8b40f8a14cb

Let's break down what we just used and see how it appears in the section of the page when rendered in a browser:

See how it turned time() into an actual timestamp?

 

Notice how

get_template_directory_uri() . '/assets/stylesheets/tomorrow-night.css

was turned into

http://www.freshlondon.digital/wp-content/themes/freshlondon/assets/stylesheets/tomorrow-night.css

 

Any comments or suggestions? Leave your thoughts!

© 2021 - FreshLondon