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.

I have trawled the net looking for an answer to this, but for some reason all I can find are actual examples, but without that particular explanation, which is clear say in the case of scripts. Can someone explain to me why it's important/advantageous to enqueue styles when developing a theme, rather than just using <link rel=stylesheet> in header.php.

Also, what about the theme's default stylesheet, should that be enqueued too?

share|improve this question
    
it is encouraged by the theme review requirements. codex.wordpress.org/… though optionally you CAN just link rel= the main stylesheet in the header –  helgatheviking Mar 7 '12 at 22:08
1  
The default stylesheet (ie: style.css in root of your theme's directory) is automatically loaded, so doesn't need to be enqueued. If all your CSS for your theme is contained in that stylesheet (or if you use 'import' rules in that stylesheet to load CSS), then no further enqueueing is required. However, for greater flexibility within child themes, it can be advantageous to conditionally enqueue additional CSS that can be omitted by any child theme, as @kaiser illustrates. –  Tom Auger Mar 14 '12 at 12:37

3 Answers 3

up vote 16 down vote accepted

It's important to enqueue the stylesheet because it will allow child themes the flexibility of dequeueing it as well as allowing it to be listed for dependencies and a multitude of other things. It just generally allows greater flexibility, both for you and for any other developers who interact with your code.

It's also important to note that using the default stylesheet in your theme is not a requirement. The stylesheet must be PRESENT, but if you use it for nothing more than to name your theme, set the version, etc, wordpress is COMPLETELY fine with that, as are the people who approve themes for the main repository.

share|improve this answer

Another reason for enqueuing is that it lets plugins do things with the styles. For instance, Better WordPress Minify will automatically and concatenate CSS files, and WP-LESS will compile your LESS files and cache them on the fly. They both do this by hooking into the styles queue and processing the files lined up there.

There may be specific reasons why you need certain styles to be excluded from things like this, but generally it's good to make your styles available to such useful functionality.

share|improve this answer

As an addition to the other answer by @m0r7if3r:

You can use current_theme_supports() to only load the parent themes stylesheet if there's theme support.

function add_supported_stylesheets()
{
    if ( current_theme_supports( 'parent-stylesheet' ) )
        wp_enqueue_style( 'main', get_stylesheet_directory_uri().'/style.css', array(), filemtime( get_stylesheet_directory().'/style.css' );
}

// In your parent themes bootstrap in the functions.php file
// Add the theme support:
add_theme_support( 'parent-stylesheet' );
// Then add the stylesheet:
add_action( 'after_setup_theme', 'add_supported_stylesheets', 20 );

Note, that this function adds filemtime on the version-nr. to prevent browser caching if the files contents were changed.

This will allow your users to disable the stylesheet in the child themes bootstrap with a simple single fn call:

remove_theme_support( 'parent-stylesheet' );
// ...or...
add_theme_support( 'parent-stylesheet' );
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.