I need to add some scrips for Google Maps only in my contact page and I do not want to populate it on all pages. I am not willing to create a custom header as well so I was thinking is there any way to populate the js file only on contact page?
currently I am using this code to add javascript to theme

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

but as I said I have another file called googlemap.js can you please let me know if I can add it to ONLY contact page from this code? the other Point is WordPress is adding this code at the end of body (before tag) which honestly I do not know how and why! but in my specific case I need to add the code into ! can you please let me know how I can do this

Thanks

2 Answers
2

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 Reply

Your email address will not be published. Required fields are marked *