jeremy-h of Web Development
12/28/2017 - 10:54 PM

Caching Fix (Force Timestamp as File Version Numbers)

WordPress has a common problem where updating the style.css will not be forced on viewers. They will have to refresh their page before viewing your style changes.

This code attempts to add a version number to the end of the document based on the timestamp the file was last saved. The version number is seen in the DOM and so forces the file to be re-cached.

// SOURCE for Options 1-2:
// SOURCE for Option 3:


// 1. wp_enqueue_style (this next line is usually all I ever need)
wp_enqueue_style('site_responsive', get_template_directory_uri() . '/style.css', false, filemtime(get_stylesheet_directory() . '/style.css'));

wp_enqueue_style('scrollbars', get_template_directory_uri() . '/assets/css/jquery.mcustomscrollbar.css', false, null);
// 2. wp_register_script

//First Register the Script
wp_register_script('js_main', get_template_directory_uri() . '/assets/js/main.js', false, filemtime( get_stylesheet_directory().'/assets/js/main.js' ), true);
//Then Enqueue It

// 3. HTML (not preferred method)
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); echo '?' . filemtime( get_stylesheet_directory() . '/style.css'); ?>" type="text/css" media="screen, projection" />