Skip to content

Instantly share code, notes, and snippets.

@jshwlkr
Last active April 7, 2022 19:06
Show Gist options
  • Save jshwlkr/9ea2a48c8c7e3b4cda3805f3bb05aa42 to your computer and use it in GitHub Desktop.
Save jshwlkr/9ea2a48c8c7e3b4cda3805f3bb05aa42 to your computer and use it in GitHub Desktop.
Append a random string to WordPress static assets, to bust a cache, if you are not on a production server.
function prefix_enqueued_assets() {
$version_string = NULL;
if ( 'production' !== wp_get_environment_type() ) {
$version_string = bin2hex(random_bytes(4));
}
wp_register_style(
'prefix-theme-css',
get_stylesheet_directory_uri() . 'style.css',
array(),
$version_string
);
wp_enqueue_style( 'prefix-theme-css' );
}
add_action( 'wp_enqueue_scripts', 'prefix_enqueued_assets' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment