Wordpress - how to add version of style.css in wordpress

Version number is a parameter of wp_enqueue_style().

As per the Codex, here are all the parameters that wp_enqueue_style accepts.

wp_enqueue_style( $handle, $src, $deps, $ver, $media );

So for example to load a stylesheet with a version number you'd do the following:

function wpa_90820() {
    wp_enqueue_style('my-styles', get_stylesheet_directory_uri() .'/my-styles.css', array(), '1.0' );       
}

add_action('wp_enqueue_scripts', 'wpa_90820');

Instead of hardwiring the version, you might find it better in some instances to dynamically version your stylesheet so whenever you change it, it automatically changes and refreshes the browser cache immediately without having to edit your functions.php over and over again.

You can use filemtime() to do that. Here is how to do that in a child-style that references the parent_style

    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array( $parent_style ), filemtime( get_stylesheet_directory() . '/style.css' )  );

If you are a theme developer, you might want to force reload of your assets when you push new version.

So versioning of a theme is done in style.css

/*
    Theme Name: Your Theme Name
    Version: 1.0.2
*/

At the top of your functions.php:

$theme = wp_get_theme();
define('THEME_VERSION', $theme->Version); //gets version written in your style.css

Later when you enqueue CSS or JS, use THEME_VERSION as fourth argument:

function theme_styles()
{
    wp_enqueue_style('main', get_template_directory_uri().'/css/main.css', [], THEME_VERSION, 'all');
}
add_action('wp_enqueue_scripts', 'theme_styles'); 

Will output to the page:

.../your-theme-name/css/main.css?ver=1.0.2 

Gets handy when you have more assets to care about and don't want to change them manually.

Tags:

Css

Themes