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.
Leave a Reply
You must be logged in to post a comment.
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.
You must be logged in to post a comment.
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 usestyle.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:This will load your alternative stylesheet as the page’s stylesheet and completely ignore the regular
style.css
.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’sfunctions.php
:Requires that your
header.php
does awp_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.
Drop this in your theme’s functions.php file:
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
And then when you make the link to your style sheet you can do this.
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
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
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:
(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:
Note, that I provided
null
as a version instead offalse
, 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:
After I change my theme to Version 2.0.0 in my style.css it would output the following:
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
functions.php
Well, you could simply use
style.css
as the place where you call the version you want. Simply putThen when you upgrade a version, just edit it to be:
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.
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' )
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.In
functions.php
changeto
change
$ver
to any value, ortime()
for development mode.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):
And open up
header.php
. In this file find this line:To add a ‘version’ to the linked stylesheet (assuming you want it to be something like:
stylesheetURL.css?version=2
) change it to: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:(Basically removing the
<?php ... ?>
and replacing it with a regular path.)