Avoid caching issues when debugging WordPress themes scripts or style sheets

Automatically append the current time onto the end of queued scripts and styles when debug mode is enabled.

You may have experienced times when reloading your webpage, expecting updated styles to take effect but they don’t due caching.

To help avoid browser caching issues when developing in WordPress, automatically append the current time to the end of queued stylesheets or scripts only when debug mode is enabled.

/**
 * Enqueue Theme styles and scripts
 * If WP_DEBUG = true then break the browser cache by setting version as time.
 */
function jc_enqueue_scripts(){

	$ver = 1;
	if(true === WP_DEBUG){
		$ver = time();
	}

	wp_enqueue_style('theme-stylesheet', get_stylesheet_directory_uri() . '/style.css', array(), $ver);
}
add_action('wp_enqueue_scripts', 'jc_enqueue_scripts');

Leave a Reply

Fields marked with an * are required to post a comment