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.

Wordpress already comes with jQuery. But what if I want to use a jQuery plugin on the entire blog? What's the best way to include on the entire site?

And what if I only want to include it on one particular post/page?

share|improve this question

migrated from webmasters.stackexchange.com Aug 26 '11 at 3:22

This question came from our site for pro webmasters.

2 Answers 2

A better method is to use the built in WordPress hooks in your functions.php with conditionals if needed ( for instance to load it only on certain pages).

For hooking into admin you can use admin_print_scripts for hooking into the front you can use wp_print_scripts or one of the many other action hooks.

For example:

   // This is site wide ( I think)

    function add_your_scripts() {
        wp_enqueue_script('jquery');
    }
    add_action('wp_print_scripts', 'add_your_scripts');


   // same example but in admin only

    function add_your_scripts() {
        wp_enqueue_script('jquery');
    }
    add_action('admin_print_scripts', 'add_your_scripts');

A great example from codex deregister the built in jquery.js and registers the one hosted on google,

function my_scripts_method() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
    wp_enqueue_script( 'jquery' );
}    

add_action('wp_enqueue_scripts', 'my_scripts_method');

Also not there is a filter suffix for some of these hooks ( I just learnt about this)

For instance if you want to load scripts only on 1 page you can (using first example above).

add_action('wp_print_scripts-yourcustom-page.php', 'add_your_scripts');
share|improve this answer

Assuming you're following the overall rules for theme creation, use wp_enqueue_script() in header.php somewhere above the call to wp_head(), eg:

<?php wp_enqueue_script('jquery'); ?>
<?php wp_head(); ?>

That doc page has a list at bottom of all the scripts that come bundled along with WordPress. Here's a more-focused walkthrough at Digging into WordPress.

And what if I only want to include it on one particular post/page?

It wouldn't be hard(cf. is_page() and similar functions), but it probably wouldn't be worth the effort, either. If you can provide some more specific reason you'd want to do this, that might change, but overall: don't bother.

What if later on you decide to have it on one more page? Then one more. And then maybe just those pages but also categories, and so on. Just include it globally and then use it or not. Beyond the first pageload, visitors will be using their cached copy and it won't be slowing anything down enough to matter.

share|improve this answer

Your Answer

 
discard

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