0

Assume that we have a theme named parent_theme. This theme's style.css will be automatically included in the styles we deliver to the client.

My question is - when Wordpress "includes" style.css, what $handle does it use? By $handle, I mean within the context of wp_enqueue_style(). I started digging through the code to find the answer, but thought I'd ask here first.

I would assume it's the "stylesheet name", aka the name of the directory that contains the theme. I tried that, but it doesn't seem to work.


(Also, if you're curious, I'm asking because I have a child_theme based on parent_theme, and I need to make sure that all of the dependencies are correctly sorted out regardless of whether parent_theme is in use or child_theme is in use. I can get very close to what I want by simply leaving the $deps parameter empty in my wp_enqueue_style() calls in parent_theme, but it would be awesome to have a little more fine-tuning control).

  • Also, it's possible that the ultimate answer is, "you're crazy; you shouldn't ever need to do that". If that's the case, feel free to say so! Although if there is, indeed, a $handle used for the default style.css, I'd love to know! :) – rinogo Sep 26 '17 at 23:32
1

Themes' style.css files are not enqueued for printing automatically (see the developer handbook) - instead this responsibility is left to the theme developer. As such, you'll need to delve into the parent theme's source files to determine the appropriate handle. That said, developers sometimes default to using 'theme' or 'style' handles.

Note that the developer may have opted to load the stylesheet directly in their header file, or a function hooked to wp_print_styles/wp_head by explicitly printing an appropriate <link> element, in which case there will be no handle and you may need to replicate the corresponding function/template in your child theme or alter action priorities in order modify the loading process.

Further, if your child theme's files prevent the parent theme's style.css from enquing in the first place, you'll need to manually register or enqueue the parent theme's style.css if you wish to use it as a dependency. get_template_directory_uri() is useful for this task:

wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
  • Thank you! Your comments are spot on. Furthermore, they led me on a wild Google-chase to this SO answer about the tendency for themes to include style.css via a <link> element. Turns out that is exactly what my base theme is doing. Seems a little inconsistent to use a combination of <link> as well as wp_enqueue_style(), but that's exactly what's going on. Thanks again! :) – rinogo Sep 27 '17 at 1:31
  • 1
    I'm familiar with such goose chases :P - sadly stylesheet enquing lacks standardization across the board. WordPress auto-loads locale and text-direction specific stylesheets from a theme's directory, for instance, but for whatever reason they're not enqueued. Maybe one day ¯\_(ツ)_/¯ – bosco Sep 27 '17 at 1:43
  • Blech. As I'm diving in more, I'm finding that this theme mixes equal parts of <link> CSS and wp_enqueue_style() CSS. I don't see any big advantage to using <link>. It just seems to complicate things; am I missing something obvious? – rinogo Sep 27 '17 at 2:43
  • BTW, I thought I'd make this an actual question - feel free to chime in for even more fake internet points! :) – rinogo Sep 27 '17 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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