As a web developer, managing and optimizing your website’s assets, such as stylesheets and scripts, can be challenging. A common issue is ensuring users receive the most up-to-date versions of these resources after changes are made. Browsers cache CSS and JavaScript files for faster loading times, which can result in users seeing outdated versions even after updates.
Name/Host: _dmarc.booking.mikah.digital
Type: TXT
TTL: 3600
Value:
"v=DMARC1; p=none; rua=mailto:dmarc@booking.mikah.digital; pct=100; fo=1; adkim=r; aspf=r"
This problem is typically referred to as cache busting. Thankfully, WordPress provides several functions to handle this, and one particularly useful function is filemtime()
. In this post, we will explore the filemtime()
function in detail, focusing on how it can be leveraged within a WordPress theme to address caching issues effectively. We will also touch upon the get_stylesheet_directory()
function, which is essential for correctly enqueueing stylesheets in a WordPress environment.
What is filemtime()
?

The filemtime()
function in PHP is a built-in function that retrieves the last modified time of a specific file. Simply put, it returns a Unix timestamp representing the last modification date and time of a file. This is incredibly useful in WordPress development when working with stylesheets and scripts, as you can append this timestamp to the file URL, ensuring that browsers load the most recent version of the file if it has been modified.
$file_mod_time = filemtime(get_stylesheet_directory() . '/style.css');
This will fetch the last modified time for the style.css
file.
Importance of filemtime()
in Cache Busting
The core functionality of filemtime()
helps solve the caching problem effectively. When you enqueue styles or scripts in WordPress, the standard approach is to add a version parameter to the URL. By using filemtime()
as the version number, as demonstrated in the example provided, you ensure that whenever style.css is updated, the timestamp will change. This change will signal the browser to fetch the new version instead of relying on the cached version.
function piassa_scripts() { wp_enqueue_style('stylesheet', get_stylesheet_uri(), [], filemtime(get_stylesheet_directory() . '/style.css'), 'all'); } add_action('wp_enqueue_scripts','piassa_scripts');
In this code, the piassa_scripts
function enqueues a stylesheet and uses filemtime()
to fetch the last modified time of the stylesheet. This timestamp is used as the version number. As a result, whenever style.css is modified, the new timestamp will generate a new URL, prompting the browser to download the latest version.
Benefits of Using filemtime()
Function
- Automatic Cache Busting: By using the last modified time, you automate the cache-busting process. You don’t have to manually update the version number every time you modify your styles or scripts.
- Improved User Experience: Users will always see the latest design and functionality as they will receive the most recent versions of the stylesheets and scripts, leading to fewer issues related to outdated assets.
- Performance Optimization: Although this method prompts browsers to fetch the latest file on updates, caching is still maintained when files haven’t changed, leading to optimized load times.
Conclusion
In conclusion, the filemtime()
function is an invaluable tool for WordPress developers looking to manage cache effectively and ensure users receive the latest versions of assets. When paired with get_stylesheet_directory()
, it enhances the process of loading dynamic CSS files in your themes. By understanding and implementing these functions, you can ensure a smoother, faster, and more reliable experience for your site visitors. In the ever-evolving landscape of web development, strategies like these are crucial for maintaining a high standard of user satisfaction and operational excellence.