2268 posts
  • Elite Author
  • Author Level 10
  • Exclusive Author
  • Winner
+16 more
mpc
says

Hi, What is the best way to include jQuery into WP Plugin? I am using the wp_register_script() function but what add_action tag should I use?

353 posts
  • Featured Item
  • Trendsetter
  • Winner
  • Netherlands
+9 more
RikdeVos
says
I always use something like this:
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
wp_enqueue_script( 'jquery' );
2268 posts
  • Elite Author
  • Author Level 10
  • Exclusive Author
  • Winner
+16 more
mpc
says
RikdeVos said
I always use something like this:
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
wp_enqueue_script( 'jquery' );

Ok but when do you call this?

353 posts
  • Featured Item
  • Trendsetter
  • Winner
  • Netherlands
+9 more
RikdeVos
says
<?php function my_scripts_method() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');
    wp_enqueue_script( 'jquery' );
}    

add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

Just hook it to the wp_enqueue_scripts action :)

2268 posts
  • Elite Author
  • Author Level 10
  • Exclusive Author
  • Winner
+16 more
mpc
says

Thanks! :)

517 posts Abid Omar
  • 7 Years of Membership
  • Affiliate Level 4
  • Author Level 7
  • Beta Tester
+9 more
omarabid
says

I wouldn’t recommend this code. You are essentially changing the jQuery version and breaking some installed plug-ins that might not be compatible. A better solution is to mention the required jQuery version.

3535 posts
  • Power Elite Author
  • Author Level 12
  • Trendsetter
  • United States
+13 more
sevenspark
Moderator
says

If you’re talking about a plugin that will be distributed, you really shouldn’t deregister the default jQuery library, as it might load an incompatible version.

That is, when you have a custom install it’s great to use that code that RikdeVos suggested for your site, as long as you’re sure all your plugins and theme will work with it. Otherwise, your plugin is likely to break other themes/plugins if they are “expecting” a different jQuery library.

For example, if you’re selling this script and a customer is running WordPress 3.0 still and you deregister the default jQuery that comes with that version (1.4.2 I believe), you’re liable to break all sorts of things that worked in 1.4.2 but don’t work in 1.6. Moreover, the code won’t be futureproof – if the next version of WordPress includes a new version of jQuery, new features might not work if you use this code.

So you should rely on WordPress to load the proper jQuery for its version in my opinion :) In which case it’s as simple as this:

function my_scripts_method() {
    wp_enqueue_script( 'jquery' );
}    

add_action('wp_enqueue_scripts', 'my_scripts_method');

An even better solution might be to detect the version of WordPress and load the appropriate Google CDN jQuery library for that version. Hmm, I wonder if anyone does this?

Chris

EDIT : To put it another way, a single plugin shouldn’t exert control over an integral piece of the entire WordPress installation.

2054 posts
  • Affiliate Level 3
  • Author Level 11
  • Collector Level 2
  • Elite Author
+11 more
bitfade
says

Do not mess with jquery and just use the one bundled with wp.

Any plugin deregistering jQuery should just be rejected.

376 posts
  • Trendsetter
  • Weekly Top Seller
  • Elite Author
  • Author Level 10
+12 more
RightHere
says
sevenspark said
To put it another way, a single plugin shouldn’t exert control over an integral piece of the entire WordPress installation.

I can’t agree more. This is really important 9 out of 10 times if one of our plugins doesn’t work. It is because a theme or other plugin loads its own jQuery version instead of using the one that WordPress uses.

We’ve made sure that our plugins use the jQuery that is bundled with WordPress. ;)

2268 posts
  • Elite Author
  • Author Level 10
  • Exclusive Author
  • Winner
+16 more
mpc
says
RightHere said
sevenspark said
To put it another way, a single plugin shouldn’t exert control over an integral piece of the entire WordPress installation.

I can’t agree more. This is really important 9 out of 10 times if one of our plugins doesn’t work. It is because a theme or other plugin loads its own jQuery version instead of using the one that WordPress uses.

We’ve made sure that our plugins use the jQuery that is bundled with WordPress. ;)

So maybe some quick tip on how to do this?

by
by
by
by
by
by