Ensuring WordPress Efficient Enqueue with reliable cache busting is essential, especially for developers creating themes or plugins for marketplaces or open-source use. Many developers rely on version numbers in WordPress themes or plugins for cache busting, meaning the cache only clears when the version changes. Otherwise, a hard reload is needed, which can slow down the development process as developers must frequently hard reload to see updates. This repeated process is time-consuming and can impact workflow, especially during active development.
In my point of view, filemtime()
ensures cache busting smartly by using the file’s last modification time as the version number. The filemtime()
function is used to get the last modification time of a file. This function returns the exact date and time when the file’s content was last updated, making it especially useful for cache busting. By adding this timestamp to your file version, you ensure browsers always load the latest version of your file whenever you make changes.
Let’s dive into the code! Simply add the code below to your theme’s functions.php
file.
function techidem_theme_enqueue_assets() {
// Enqueue main stylesheet
wp_enqueue_style( 'main-style', get_stylesheet_uri(), array(), filemtime( get_template_directory() . '/style.css' ) );
// Enqueue additional stylesheets
wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.min.css', array(), '5.3.0' );
// Conditionally load a script for a specific page
if ( is_page('contact') ) {
wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY', array(), null, true );
}
// Enqueue a custom script, loaded in the footer
wp_enqueue_script( 'custom-script', get_template_directory_uri() . '/assets/js/custom.js', array('jquery'), filemtime( get_template_directory() . '/assets/js/custom.js' ), true );
// Example of adding inline script data
wp_localize_script( 'custom-script', 'siteData', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('my_nonce')
));
}
add_action( 'wp_enqueue_scripts', 'techidem_theme_enqueue_assets' );
The main style is enqueued using, get_stylesheet_uri()
and filemtime()
ensures cache busting by using the file’s last modification time as the version number.
Here’s a code snippet specifically designed for use in a WordPress plugin. This example demonstrates how to efficiently enqueue styles and scripts with dynamic cache busting. By using this code in your plugin, you can ensure that any updates to your CSS or JavaScript files are automatically reflected for users without requiring a hard refresh.
// Define constants for plugin directory and URI
if ( ! defined( 'MY_PLUGIN_DIR' ) ) {
define( 'MY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'MY_PLUGIN_URI' ) ) {
define( 'MY_PLUGIN_URI', plugin_dir_url( __FILE__ ) );
}
// Enqueue styles and scripts with cache busting
function techidem_plugin_enqueue_assets() {
// Enqueue style with cache busting
wp_enqueue_style(
'my-plugin-style',
MY_PLUGIN_URI . 'assets/css/style.css',
array(),
filemtime( MY_PLUGIN_DIR . 'assets/css/style.css' ) // Dynamic version based on last modified time
);
// Enqueue script with cache busting
wp_enqueue_script(
'my-plugin-script',
MY_PLUGIN_URI . 'assets/js/script.js',
array( 'jquery' ),
filemtime( MY_PLUGIN_DIR . 'assets/js/script.js' ), // Dynamic version based on last modified time
true
);
}
add_action( 'wp_enqueue_scripts', 'techidem_plugin_enqueue_assets' );
Note: You don’t need to worry about cache busting for third-party styles or scripts, such as Bootstrap, Google Maps, and similar resources. These libraries are typically managed by external servers, which handle their own cache updates, so you can rely on them to stay current without any extra steps.