How to correctly load JavaScript / jQuery into WordPress

Below is the correct way to load JavaScript / jQuery into your WordPress Website. Place the below custom code into your themes child function.php file. Just to note “custom.js” is the file we load from the theme folder.

// ADD CUSTOM JS
function wpb_adding_scripts() {
	wp_register_script('my_amazing_script', get_stylesheet_directory_uri() . '/custom.js', array('jquery'),'1.1', true);
	wp_enqueue_script('my_amazing_script');
}

add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );  
Posted in