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

Bootstrap CDN

Is it bad practice to just add the bootstrap CDN to the header.php?

2 Answers

Kevin Korte
Kevin Korte
28,148 Points

No, it's not bad practice, but there are pros and cons to using the CDN

Pros

  • It should be delivered faster to the browsers, which means it should load faster.
  • If your visitor was on a different website that also loaded Bootstrap from that CDN, it's likely their browser cached a version of it, and instead of reloading the exact same thing, it'll just use the cached version, increasing the perceived speed of your site

Cons

  • If the CDN is down, you don't have that framework.
  • You're pretty much stuck with the entire bootstrap framework CSS, whether you need it or not.

I would be okay having the CDN file as the primary file, but I would also have a local copy of bootstrap on the server, and if the CDN ever failed, I would have the browser fallback to using the local version. That way you're pretty well protected.

Sue Dough
Sue Dough
35,800 Points

Yes it is bad practice.

Kevin Korte
Kevin Korte
28,148 Points

I disagree. Or else why would Google's CDN be so popular? Almost every site pulls in jQuery from Google's CDN. Are you saying all of those sites are using bad practices? I stand by my list of advantages and disadvantages.

Sue Dough
Sue Dough
35,800 Points

Well your wrong. You clearly don't understand the question and your preaching bad practice on the forums. I never said using Google's CDN is bad practice. But putting the script in your header.php in wordpress is bad practice. I know Google says to do that but that's not the recommended way for wordpress. The get_header function calls the enqueued scripts from the functions.php file.

Kevin Korte
Kevin Korte
28,148 Points

You're right, I didn't read the question correctly. To just dump the CDN into the header.php is bad practice. I'll agree. That was a mistake on my end. Thank you for pointing that out.

Sue Dough
Sue Dough
35,800 Points

Yes its bad practice. You don't ever want to include scripts directly in your header.php . You want to enqueue scripts and styles in your functions.php .

Kevin Korte
Kevin Korte
28,148 Points

How is enqueueing scripts and using a CDN related, exactly? This is perfectly valid for wordpress

<?php wp_enqueue_style( 'bootstrap_cdn', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css', '', '', '' ); ?>

To quote WP's codex. Remote assets can be specified with a protocol-agnostic URL, i.e. '//otherdomain.com/css/theirstyle.css'.

Sue Dough
Sue Dough
35,800 Points

You don't put the enqueued scripts in your header.php file and that is clearly what the question is asking.

Kevin Korte
Kevin Korte
28,148 Points

You're right. I didn't read it correctly.