Wordpress - Attributing a version number to a child theme's main stylesheet
I think the best way to do this is to leave the child theme stylesheet (style.css) empty with only the necessary comments (like theme name, description etc, so can wordpress recognize your theme) and then make another css file in your-theme-name-folder/css/main.css
After that on function.php you can have a new "version" every time you change your file :
function my_scripts_and_styles(){
$cache_buster = date("YmdHi", filemtime( get_stylesheet_directory() . '/css/main.css'));
wp_enqueue_style( 'main', get_stylesheet_directory_uri() . '/css/main.css', array(), $cache_buster, 'all' );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_and_styles', 1);
The logic:
Every time you save the file the modified time of the file is being changed. The new time is being passed to date function to convert the time(filemtime returns an integer representing the time) to date format to make it a string in the format you desire. In our example the time is being formatted with accuracy of minutes. You can change it to track even second ie "YmdHis"
.After that the new modified time of the file is being passed as a new version to wp_enqueue_style
.
Reference :
http://www.php.net/filemtime
http://php.net/manual/en/function.date.php
What you need to do is de-register the main style by handle and then re-register with your version number. In this case the handle is style-css
.
You can determine the handle that you need to use by looking at the rendered stylesheet link:
<link rel='stylesheet' id='style-css-css' href='http://site-url/wp-content/themes/child-theme/style.css?ver=4.6.1' type='text/css' media='all' />
Here the id is style-css-css
which means our handle is style-css
Put this in the function.php of your child-theme:
function wpse_145141_change_style_version(){
// First de-register the main stylesheet
wp_deregister_style( 'style-css' );
// Then add it again, using your custom version number
wp_register_style( 'style-css', get_stylesheet_uri(), array(), "VERSION_NUMBER" );
//finally enqueue it again
wp_enqueue_style( 'style-css');
}
add_action( 'wp_enqueue_scripts', 'wpse_145141_change_style_version');
You can update the version in the child themes style sheet itself...
/*
Theme Name: Twenty Fourteen Child
Theme URI: http://example.com/twenty-fourteen-child/
Description: Twenty Fourteen Child Theme
Author: John Doe
Author URI: http://example.com
Template: twentyfourteen
Version: 1.1.2 <---- Update here
*/