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 trial

WordPress From Bootstrap to WordPress Setup a Bootstrap Theme Add Bootstrap CSS via the functions.php File

Dayne Wright
Dayne Wright
11,235 Points

Why 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
Kevin Korte
28,148 Points

It'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
Dayne Wright
11,235 Points

Ok. 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
Kevin Korte
28,148 Points

More 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
Kevin Korte
28,148 Points

I'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.