1

I realise you can enqueue JS in the footer using wp_register_script as there is a $in_footer parameter for doing so. However, there isn't one for wp_register_style it seems.

Is there a way to enqueue CSS in the footer in the same way, or do I have to manually hardcode the file path in my footer.php file?

3
  • Any reason you need to do this? Commented Sep 13, 2014 at 8:18
  • 2
    Yes. Stylesheets should be added to header.
    – Robert hue
    Commented Sep 13, 2014 at 8:33
  • 1
    <style> tag anywhere outside <head> is invalid (if not scoped).
    – Sisir
    Commented Sep 13, 2014 at 10:38

1 Answer 1

2

Technically styles should only be output in head and outputting them elsewhere is invalid HTML (browsers are however very lax about it and don't care much).

So in best interest of standards WordPress wouldn't do something like that. Right? Right!?

It does without bit of remorse actually. There is print_late_styles() function, running in footer that will output any queued styles that hadn't appeared in the header.

So while there is no explicit way to enqueue style for footer (like for scripts), simply adding them into queue after header had passed will make them appear there.

Still not too good of an idea, but it does work.

4
  • Thanks for the clear explanation (and to everyone else for their input). My only reason for considering it was to try and speed things up on the page, but obviously if it's bad practice I'll scrap the idea!
    – Osu
    Commented Sep 13, 2014 at 14:51
  • 1
    It is worth mentioning here that Google Pagespeed makes recommendations to only load critical "above the fold" CSS in the header, because CSS in the header is render-blocking. Then after the above the fold content has loaded, you can load the rest of your styles. Read more here: developers.google.com/speed/docs/insights/OptimizeCSSDelivery
    – Matt Keys
    Commented Jan 22, 2015 at 0:05
  • 1
    @MattKeys I wouldn't use grossly invalid HTML because Google said to, personal opinion.
    – Rarst
    Commented Jan 22, 2015 at 8:29
  • 1
    This could probably use an update, as the spec now says that using rel="stylesheet" in the body is ok.
    – jhned
    Commented Apr 24, 2020 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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