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?

  • Any reason you need to do this? – Pieter Goosen Sep 13 '14 at 8:18
  • 2
    Yes. Stylesheets should be added to header. – Robert hue Sep 13 '14 at 8:33
  • 1
    <style> tag anywhere outside <head> is invalid (if not scoped). – Sisir Sep 13 '14 at 10:38
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.

| improve this answer | |
  • 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 Sep 13 '14 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 Jan 22 '15 at 0:05
  • 1
    @MattKeys I wouldn't use grossly invalid HTML because Google said to, personal opinion. – Rarst Jan 22 '15 at 8:29
  • This could probably use an update, as the spec now says that using rel="stylesheet" in the body is ok. – jhned Apr 24 at 14:00

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.