Clear thinking · Useful delivery · Work built to last

PEN BEAM / COPY-READY EXAMPLE

Enqueue a WordPress Stylesheet with Cache Busting

PHPWordPressWordPress 5.0+

Use filemtime() to version a local theme stylesheet so browsers request updated CSS after a file changes.

What this snippet does

Enqueues a local stylesheet using its modification time as the version, helping browsers request updated CSS after a file changes.

PHP / source code
add_action('wp_enqueue_scripts', 'pb_example_enqueue_styles');
function pb_example_enqueue_styles(): void {
    $relative_path = '/assets/css/site.css';
    $absolute_path = get_stylesheet_directory() . $relative_path;
    $stylesheet_uri = get_stylesheet_directory_uri() . $relative_path;
    wp_enqueue_style(
        'pb-example-site',
        $stylesheet_uri,
        [],
        is_file($absolute_path) ? (string) filemtime($absolute_path) : null
    );
}

Where to place it

Place this code in a child theme functions.php or a site-specific plugin that owns front-end asset registration. Change the file path and handle to match your theme.

Back up the relevant file or settings first. Apply the change on staging, verify behavior, and retain a rollback path before production deployment.

What to check before deploying

  • Confirm the path exists in the active child theme; use get_template_directory*() if the asset is intentionally owned by the parent theme.
  • Check the Network panel to verify the URL version changes after the stylesheet changes.
  • Use a build-generated version for distributed systems where local file modification times may not match across servers.

Related snippets

Need this adapted to your project? Start a project