Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDayne Wright
11,235 PointsWhy do you use functions.php wp_enqueue_style for CSS instead of just <script> tags?
I am just curious as to why you would use the functions.php file to include CSS files through wp_head() instead of just placing a <script> tag on the page. Is there a reason for this?
2 Answers
Kevin Korte
28,149 PointsIt's crowd control, basically. With all of the plugins and such, it's hard to manually make sure that for every wordpress site, there isn't going to be conflicts because you and a plugin are both loading in the same dependency file, or that you want to make sure that all dependencies are loaded first.
Because there can be so many things going on dynamically, wordpress developed a really great way for the plugin author(s), theme author(s), and wordpress site owner(s) to put all of their assets into a pool, ensuring dependencies get loaded in the correct order, and that wordpress itself would figure out the rest.
Dayne Wright
11,235 PointsOk. Just to make sure I understand this:
By using the functions file and loading it that way you get more control over the order and how things are loaded vs. just listing them on the DOM?
Kevin Korte
28,149 PointsMore or less. You're allowing wordpress to ensure you do not have file conflicts or missing dependencies. It's an even playing field for all plugin and theme authors to make sure their scripts get in where they need to be without affecting each other.
Kevin Korte
28,149 PointsI'll admit, there is a little bit of smoke and mirrors here, and I know why to do it, but not how wordpress actually does it. If you were really curious, you could go read the functions that do the enqueueing in the core of wordpress, that may or may not help you understand what is actually happening. This is a valid question.
Dayne Wright
11,235 PointsThanks!
This helps as well: http://www.paulund.co.uk/add-stylesheets-to-wordpress-correctly