Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

7 Answers 7

up vote 3 down vote accepted

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.

share|improve this answer
    
Just to clarify, you DO have to have a style.css file in your WordPress theme with the theme name in it. You DON'T have to load that file in your header.php file. –  Doug Aug 12 '10 at 16:58
    
And you shouldn't put your styles in theme files directly - use wp_enqueue_style instead - it's much nicer for child themes and plugins. –  Krzysiek Dróżdż Dec 11 '13 at 20:58

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.)

share|improve this answer
1  
"This is kind of inelegant, though" - yeah, particularly if I want to use a file named "style-1.css"! ;-) –  Bobby Jack Aug 11 '10 at 23:11
    
@Bobby Jack, agreed and +1 (though I did try and add a similarly inelegant approach to achieve your aim.) =/ –  David Thomas Aug 11 '10 at 23:13
    
OK. I guess I was concerned that stylesheet_url might be used elsewhere, but there's really no reason for that, is there? There's also the fact that it would be nice to have some mechanism for automating the update to always use the most recent file version, but I'm just starting to expose flaws in my question now :) –  Bobby Jack Aug 11 '10 at 23:27

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' );
share|improve this answer
    
Presumably, I would need to call that function from somewhere ... (and I assume the missing / is a mistake!) –  Bobby Jack Aug 11 '10 at 23:10
    
Yes, that missing slash was a mistake, and I added the filter hook you'll need to use too. –  John P Bloch Aug 11 '10 at 23:18
    
Also, note that as you increase the version, the second argument ('/style-1.css') will change, but the first one ('/style.css') will always stay the same. –  John P Bloch Aug 11 '10 at 23:23
    
amazing, we should create plugin for this! –  ariefbayu Aug 12 '10 at 0:07

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.