How to Link External jQuery/Javascript files with WordPress

So I’m using Starkers to base my next WP theme on and I’ve run into a small issue, I was including my own version of jQuery in the header.php file but when inspecting my site using Firebug I noticed jquery was being downloaded twice, I did a bit of digging and noticed that not only was I including the file but so was the wp_head() function.

In trying to fix the problem I noticed a comment in the header file, of which originated came from the Twenty Ten theme:

Read More
/* Always have wp_head() just before the closing </head>
 * tag of your theme, or you will break many plugins, which
 * generally use this hook to add elements to <head>, such 
 * as styles, scripts, and meta tags
 */

So here’s my problem, I am under the impression that the jQuery file has to be set before any other file that wants to use it and that wp_head() should be the last thing in the <head> element, I’m slightly confused now as I’m wondering should I put wp_head() at the top so the WP included jQuery file will be used for all my plugins, even though it says not to do so.

I did comment out the jQuery line in the wp_head() function but it’s required for the admin page so I had to put it back.

I’d also like to use (at least experiment) with using the Google CDN version of jQuery, but don’t want to include it twice!

I hope you understand what I’m trying to explain, any suggestions on how I can solve this problem would be most appreciated. I’d also appreciate any advice on how you handle your JavaScript files with the header file.

Thanks!

Related posts

Leave a Reply

3 comments

  1. From the wording of your question, you must be adding scripts by writing <script> tags in your template. Add your own scripts via wp_enqueue_script() in your template’s functions.php, appropriately setting dependences on jQuery, and wp_head() will add the scripts for you.

    function my_scripts() {
        wp_enqueue_script( 'my-sweet-script', get_bloginfo('template_directory') . '/script.js', array('jquery') );
    }
    add_action('template_redirect', 'my_scripts');
    

    See the codex page for more info.

  2. I suggest taking a look at 5 Tips for using jQuery with WordPress. Among other things, it shows the code necessary to load jQuery from Google’s library:

    function my_init() {
        if (!is_admin()) {
            // comment out the next two lines to load the local copy of jQuery
            wp_deregister_script('jquery');
            wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2');
            wp_enqueue_script('jquery');
        }
    }
    add_action('init', 'my_init');
    

    You could also check out the Use Google Libraries plugin.

  3. Though @tnorthcutt is correct that you should properly dequeue WP’s native jquery if you want to load your own, you are certain to run into issues when you are loading a different jquery version that WP core. Both core and plugins rely on it being there. So if you don’t update your theme with the newest jquery every time WP is updated, your site may break.

    The following code will make sure your theme always loads the correct version of jquery, by first looking up which version WP is using and then loading that one from Google:

    $wp_jquery_version = $GLOBALS['wp_scripts']->registered['jquery-core']->ver;
    $jquery_version = ( $wp_jquery_version == '' ) ? '1.8.3' : $wp_jquery_version; // fallback, just in case 
    wp_deregister_script('jquery');
    wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/'. $jquery_version .'/jquery.min.js', $jquery_version, false );
    wp_enqueue_script('jquery');