Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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

    // conditional load
    is_page( 'Contact' ) {
        wp_enqueue_script('my-script');
    }
}
share|improve this answer
    
Thanks Sisir , just I have some questions here! why did you add the wp_enqueue_script(''); to the code and how come the code is adding to the body not to head? How I can force to add the my-script into the head? –  Behseini Nov 16 '13 at 9:33
    
by default scripts are loaded to the <head> not in footer. You can control this when you register a script see wp_register_script() look for $in_footer argument –  Sisir Nov 16 '13 at 9:36
    
Thanks but it is adding to body! –  Behseini Nov 16 '13 at 9:38
    
make sure you clear browse cache. Do you have any url where I can see it on action? –  Sisir Nov 16 '13 at 9:43
    
yeah www.teddybearfamilychildcare.com –  Behseini Nov 16 '13 at 9:44
show 2 more comments

Here is some tricks for you

Note:

wp_register_script(); // is only register js file, it is not running
wp_deregiser_script(); // is completely remove js file, it is not nice method is to use in page condition check

//Run Js
wp_enqueue_script(); // is use only your register js file,
wp_dequeue_script(); // this function make not to run js file, it is nice method to use in page codition check

In your functions.php file

function js_library() {

    //1. Method
    if ( is_front_page() ) {
        //wp_dequeue_script();
    }

    //2. Method
    if ( is_page(120)) { //120 is page id, you can get page id using using $post->ID or get_the_ID
        //wp_dequeue_script();
    }

    if ( is_page($post->ID) == 120 ) { //120 is your page id
        //wp_dequeue_script();
    }

    if ( is_page('about-us') ) { //about-us is using page slug, you can get page slug http://yourdomain.com/about-us
        //wp_dequeue_script();
    }

    $remove_js_pages = array('about-us', 'contact-us', 'service'); // for multi page slugs
    if ( is_page($remove_js_pages) ) { //remember you can get page slug from your page url ;-)
        //wp_dequeue_script();
    }

    //Using page name
    $pagename = get_query_var('pagename');
    if ( !$pagename && $id > 0 ) {
        // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
        $post = $wp_query->get_queried_object();
        $pagename = $post->post_name;
    }

    if ( is_page() == $pagename ) {
        //wp_dequeue_script();
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.