wp_enqueue_style, versioning and conditional CSS comments
I like to enqueue my CSS styles because it has the benefit of adding a version string. A link like style.css?ver=0.7
is useful because it will force visitors to download an updated style.css
when the version number is increased. Here's what I currently use in my functions.php
:
<?php
if ( !is_admin() ) {
$theme = get_theme( get_current_theme() );
wp_register_style( 'your-style', get_bloginfo( 'stylesheet_url' ), false, $theme['Version'] );
wp_enqueue_style( 'your-style' );
}
I don't really like the get_theme()
line, but I am not aware of a better way to get the version info. What's good about that line is that it gets the version of the current child theme, if you're using one.One problem with enqueuing styles is that most themes need one or two additional CSS files inside conditional comments for a certain browser. Nacin pointed me to a solution:
<?php
if ( !is_admin() ) {
$theme = get_theme( get_current_theme() );
wp_register_style( 'your-style', get_bloginfo( 'stylesheet_directory' ) . '/css/ie.css', false, $theme['Version'] );
$GLOBALS['wp_styles']->add_data( 'your-style', 'conditional', 'lte IE 8' );
wp_enqueue_style( 'your-style' );
}?>
See also the relevant discussion on trac. Using this could lead to script concatenation problems in the WordPress core (which doesn't concern a theme developer) and if a website uses plugins like wp-minify. So you probably only want to use this in your own themes, not public ones.
Thanks for the brief tutorial, it was helpful for me to enqueue an external sytle sheet.
going to try this soon, I've been confused about enque for styles, but this helps clear it up a bit
Brilliant article!