Add cache busting to styles and scripts

Have you ever made changes to a website and the client told you he or she can’t see the updated version? I bet you have.

The best cache busting is to append a random string to the filename. assets/main_d808a276.css
Most starter themes comes with this by default, and it’s awesome!

But if you’re working on an old installation, using query strings is a lot faster to implement. assets/main.css?v=d808a276

// Cache bust CSS files in themes
wp_enqueue_style('style.css', get_stylesheet_directory_uri() . '/style.css', [], filemtime( get_stylesheet_directory() . '/style.css' ));

// Cache bust CSS files in plugins
wp_enqueue_style('plugin-styles', plugin_dir_url( __FILE__ ) .  'assets/css/plugin-styles.css', [], filemtime( plugin_dir_path( __FILE__ ) .  'assets/css/style.css' ));

Note: Some CDNs and caching proxies can’t cache files with query strings. So even if your query string changes, they won’t update the file.