Site logo

Reply To: Child theme style.css version string

Home Forums Support Child theme style.css version string Reply To: Child theme style.css version string

Home Forums Support Child theme style.css version string Reply To: Child theme style.css version string

#62531
sleddog

All that version number does it set an expiration for browsers – when I update GP, browsers will reload the stylesheet.

Exactly. And when I update the child stylesheet I want browsers to recognize the change and reload it. This doesn’t happen when the child stylsheet has the same version as the parent stylesheet. If I set a long expires time for css files, browsers will not recognize changes to the child style.css until either the expires time is reached or you update the parent theme 🙂

As a workaround, I’ve done this is the child theme’s functions.php:

function _fix_child_css_version( $src ) {
	$parts = explode( '?', $src );
	if ( stristr( $parts[0], '-child/style.css' ) ) {
		$child_ver = filemtime( get_stylesheet_directory() . '/style.css' );
		return $parts[0] . '?v=' . $child_ver;
	}
	else {
		return $src;
	}
}
add_filter( 'style_loader_src', '_fix_child_css_version', 15, 1 );

So the version number for the child stylesheet is now the file mtime. This lets me set a long expires time for css files, yet have browsers instantly recognize changes to style.css as the version number changes when I edit/save the file.

It’s a bit kludgy, maybe you can suggest something better 🙂

Thanks.