This might seem like a basic thing, but I’m unsure on how to add jQuery (using the CDN) into the Underscores WordPress starter theme. I did some searching but didn’t really find anything, so I was hoping someone here could have an answer for me?
Thank you in advance!
The best method is to use the WordPress function
wp_enqueue_script()
in your Underscores created themefunctions.php
file.Find the function
<THEME_NAME>_scripts
(where<THEME_NAME>
is your theme name when generated from Underscores website) and prepend the code below, under<THEME_NAME>_scripts
function (before any otherwp_enqueue_script
)Underscores doesn’t enque any javascript which rely on jquery. So a default install will not load the default jquery in WordPress without it being set as a dependency when enqueing a javascript file with
wp_enqueue_script
.An example of loading bootstrap into underscore and loading jquery from WordPress is
wp_enqueue_script('bootstrap', get_template_directory_uri().'/assets/javascripts/bootstrap.min.js', array('jquery'),'v3',true);
You can do the same with a jquery file from a CDN registered in WordPress
You can use this snippet in either your header.php or footer.php:
This uses the Google hosted jQuery.
Note: WordPress ships with jQuery so if you are using Underscores as a WordPress theme then jQuery will be already enqueued.
Ref: https://developers.google.com/speed/libraries/devguide#jquery