How to Add a .js file Only in one specific Page Dynamically to Head

Use conditional is_page() for loading script on specific page.

Here Is The Modified Version of Your Function:

//Load Scripts
function load_scripts(){
    wp_deregister_script('jquery'); // De-register Existing jquery
    wp_register_script('jquery', 'http://code.jquery.com/jquery.js', '', '', true);
    wp_enqueue_script( 'jquery' );

    wp_register_script('bootstrap-jquery',get_template_directory_uri().'/assets/js/bootstrap.js', array( 'jquery' ), '', true);
    wp_enqueue_script( 'bootstrap-jquery' );

    // register the script
    wp_register_script( 'my-script', 'URL to Script' ); // by default script will load to head

    // conditionally load page
    if (is_page( 'Contact' )) {
        wp_enqueue_script('my-script');
    }
}

Leave a Comment