How can I version the main CSS file?

How can I instruct wordpress to use a filename other than ‘styles.css’ for my main stylesheet – for example, styles-1.css? I’d like to do this for versioning and caching purposes.

Related posts

Leave a Reply

9 comments

  1. Style.css is required for your WordPress theme. That’s where WordPress gets the theme name and meta information for the Appearance >> Themes menu from. That said, you don’t actually have to use style.css in your theme at all. I know of several readily available themes that don’t use it, and I only use it in a handful of my custom designs.

    In header.php just place the following tag in place of the regular stylesheet link:

    <link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_directory'); ?>/style-1.css" />
    

    This will load your alternative stylesheet as the page’s stylesheet and completely ignore the regular style.css.

  2. This may be inappropriate, please let me know if I missed something.

    The fourth argument to wp_enqueue_style() is the stylesheet’s version number. In your theme’s functions.php:

    function my_theme_styles() {
        // replace "10" with your version number; increment as you push changes
        wp_enqueue_style('my-theme-style', get_bloginfo('template_directory') . '/style.css', false, 10);
    }
    add_action('wp_print_styles', 'my_theme_styles');
    

    Requires that your header.php does a wp_head(), which of course you were doing anyway. 😉

    This allows you to push long expiry headers with your CSS file, and force clients to download a new file by updating the version number. WP will append “?ver=N” to the URL of your CSS file.

  3. Drop this in your theme’s functions.php file:

    function my_cool_style_versioner( $style ){
      return str_replace( '/style.css', '/style-1.css', $style );
    }
    
    add_filter( 'stylesheet_uri', 'my_cool_style_versioner' );
    
  4. EAMann is correct, you don’t have to use the style.css file for all your CSS.

    For versioning the style sheet and other files in your theme you can add this to your functions.php file

    function fileVersion($filename)
    {
        // get the absolute path to the file
        $pathToFile = TEMPLATEPATH.'/'.$filename;
        //check if the file exists
        if (file_exists($pathToFile)) 
        {
            // return the time the file was last modified
            echo filemtime($pathToFile);
        }
        else
        {
            // let them know the file wasn't found
            echo 'FileNotFound';
        }
    }
    

    And then when you make the link to your style sheet you can do this.

    <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>?v=<?php fileVersion('style.css'); ?>" />
    

    This way you don’t have to manually update the version number, anytime the file is updated on the server the version will automatically change to that UNIX timestamp

  5. Note, that you should not use querystrings for file versioning (proxys do not cache them).

    A better way would be to version the filenames like by adding a number like

    • style.123.css
    • style.124.css

    So my approach is the following:

    Apache htaccess redirects

    If you are using HTML5 boilerplate with apache you can find the following section in the .htaccess file:

    # ------------------------------------------------------------------------------
    # | Filename-based cache busting                                               |
    # ------------------------------------------------------------------------------
    
    # If you're not using a build process to manage your filename version revving,
    # you might want to consider enabling the following directives to route all
    # requests such as `/css/style.12345.css` to `/css/style.css`.
    
    # To understand why this is important and a better idea than `*.css?v231`, read:
    # http://stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring
    
    <IfModule mod_rewrite.c>
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.+).(d+).(js|css|png|jpe?g|gif)$ $1.$3 [L]
    </IfModule>
    

    (You usually have to enable it first, by uncommenting the lines)

    Theme functions.php

    I wanted to automatically use the version of my theme for the stylesheet, so I came up with the following:

    You can add the following to your themes functions.php:

    function my_theme_styles() {
        $my_theme = wp_get_theme();
        $version = str_replace('.','',$my_theme->get( 'Version' ));
        $stylesheet = get_bloginfo('stylesheet_directory').'/style.'.$version.'.css';
        wp_enqueue_style('my-main', $stylesheet, false, null);
    }
    add_action('wp_print_styles', 'my_theme_styles');
    

    Note, that I provided null as a version instead of false, so WordPress does not append its version in the querystring.

    Result

    This outputs a stylesheet like the following for Version 1.0.2 of your theme:

    <link rel='stylesheet' id='maw-main-css'  href='http://www.example.com/wp-content/themes/my-theme/style.102.css' type='text/css' media='all' />
    

    After I change my theme to Version 2.0.0 in my style.css it would output the following:

    <link rel='stylesheet' id='maw-main-css'  href='http://www.example.com/wp-content/themes/my-theme/style.200.css' type='text/css' media='all' />
    

    Additional notes

    Take care, that if you just strip the dots of the version like I did you may get problems with theme version like 1.2.23 and 1.22.3, as they both result in a dotless version of 1223.

    A better way would be to take that into account in the .htaccess file. Eg you could allow underscores between the numbers and could replace the dots with them.

    This is untested, but should work:

    .htaccess

    # ------------------------------------------------------------------------------
    # | Filename-based cache busting                                               |
    # ------------------------------------------------------------------------------
    <IfModule mod_rewrite.c>
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.+).([_d]+).(js|css|png|jpe?g|gif)$ $1.$3 [L]
    </IfModule>
    

    functions.php

    function my_theme_styles() {
        $my_theme = wp_get_theme();
        $version = str_replace('.','_',$my_theme->get( 'Version' ));
        $stylesheet = get_bloginfo('stylesheet_directory').'/style.'.$version.'.css';
        wp_enqueue_style('my-main', $stylesheet, false, null);
    }
    add_action('wp_print_styles', 'my_theme_styles');
    
  6. Well, you could simply use style.css as the place where you call the version you want. Simply put

    @import url("style-1.css");
    

    Then when you upgrade a version, just edit it to be:

    @import url("style-2.css");
    

    As for saving versions, have you considered using a Subversion, or git? Then you can have a complete track record of your stylesheet. It’s possible I’m not fully understanding the full reasons for your versioning.

  7. I came across this old question and found all answers to seem outdated nowadays.

    I would simply use the theme version as defined in the style.css file header part. You can get it with wp_get_theme()->get( 'Version' )

    function my_enqueue_styles() {
        wp_enqueue_style( 'my-theme-style', get_template_directory_uri() . '/style.css', false, wp_get_theme()->get( 'Version' ) );
    }
    add_action( 'wp_enqueue_scripts', 'my_enqueue_styles' );
    

    Like this, the version number will automatically be appended to the url like ?ver=#.## and the url changes as soon as the version of the theme is incremented in style.css.

  8. In functions.php change

    wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri();
    

    to

    wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri(), array(), $ver );
    

    change $ver to any value, or time() for development mode.

  9. I’m not sure if this has changed for WP3, so I’m not wholly sure, but one way is to edit the relevant php file directly (I don’t know if it can be done from within the Dashboard/Admin pages):

    wp-folder/wp-content/themes/your-theme/
    

    And open up header.php. In this file find this line:

    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    

    To add a ‘version’ to the linked stylesheet (assuming you want it to be something like: stylesheetURL.css?version=2) change it to:

    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>?version=2" type="text/css" media="screen" />
    

    This is kind of inelegant, though, so if there’s a better way I’d love to hear it myself =)


    I just re-read your question…

    To use a different stylesheet styles-1.css you can either just the (above) line to:

    <link rel="stylesheet" href="absolute/or/root-relative/path/to/styles-1.css" type="text/css" media="screen" />
    

    (Basically removing the <?php ... ?> and replacing it with a regular path.)