skip to Main Content

Snippet: Add Version Number to Child Theme to Prevent Browser Cache

This code would be added in your child theme's functions.php and what it does is re-registeres the child theme with the current style.css version number so whenever you make changes to your child theme you can update your version number and it will update the ?v= parameter in the style.css URL to prevent browser caching.

add_action( 'wp_enqueue_scripts', function() {

	if ( ! defined( 'WPEX_THEME_STYLE_HANDLE' ) ) {

	// First de-register the main child theme stylesheet
	wp_deregister_style( WPEX_THEME_STYLE_HANDLE );

	// Then add it again, using filemtime for the version number so everytime the child theme changes so will the version number
	wp_register_style( WPEX_THEME_STYLE_HANDLE, get_stylesheet_uri(), array(), filemtime( get_stylesheet_directory() . '/style.css' ) );

	// Finally enqueue it again
	wp_enqueue_style( WPEX_THEME_STYLE_HANDLE );
} );
All PHP snippets should be added via child theme's functions.php file or via a plugin (we recommend WPCode)
Back To Top