2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 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?

342 posts
  • Has referred 50+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in Netherlands
+4 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' );
2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 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?

342 posts
  • Has referred 50+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in Netherlands
+4 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 :)

2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 more
mpc says

Thanks! :)

484 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 100+ members
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+6 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.

3446 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 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.

2002 posts
  • Has referred 50+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
bitfade says

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

Any plugin deregistering jQuery should just be rejected.

370 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Located in United States
  • Has sold $250,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
+9 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. ;)

2195 posts
  • Has referred 200+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+13 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