Wordpress - How can I version the main CSS file?
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.
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' );
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
.