It should be said, that WordPress comes with jQuery out-of-the-box. You can check it, if you slap this in your page-template (based on this post):
window.onload = function() {
if (window.jQuery) {
// jQuery is loaded
alert("Yeah!");
} else {
// jQuery is not loaded
alert("Doesn't Work");
}
}
In case you need other scripts, then perhaps look into the wp_enqueue_script-function.
Only load script on certain pages
Based on your comment, then I thought I add a snippet you could try (from this page):
function custom_enqueue_scripts() {
if ( is_page_template( 'page-template-name.php' ) ) {
wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/assets/js/YOUR_CUSTOM_SCRIPT.js', array( 'jquery' ) );
}
}
add_action( 'wp_enqueue_scripts', 'custom_enqueue_scripts' );
As you can see, then the third parameter is array( jquery )
, which is an array of dependencies. Which means that your script will not be loaded before that given script (jquery) has been loaded. Super-smart! 🙂
Load script on all pages
This is if you want your script to load on all pages.
function custom_enqueue_scripts() {
wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/assets/js/YOUR_CUSTOM_SCRIPT.js', array( 'jquery' ) );
}
add_action( 'wp_enqueue_scripts', 'custom_enqueue_scripts' );