2156 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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?

335 posts
  • Netherlands
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Bought between 10 and 49 items
  • Referred between 10 and 49 users
  • Has been a member for 3-4 years
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' );
2156 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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?

335 posts
  • Netherlands
  • Sold between 10 000 and 50 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Bought between 10 and 49 items
  • Referred between 10 and 49 users
  • Has been a member for 3-4 years
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 :)

2156 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator says

Thanks! :)

484 posts
  • Bought between 10 and 49 items
  • Contributed a Blog Post
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Has been a member for 5-6 years
  • Microlancer Beta Tester
  • Referred between 100 and 199 users
  • Sold between 50 000 and 100 000 dollars
+1 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.

3362 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred more than 2000 users
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark Volunteer 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.

1988 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Author had a File in an Envato Bundle
  • Has been a member for 4-5 years
  • Author had a Free File of the Month
  • Won a Competition
  • Bought between 10 and 49 items
+4 more
bitfade says

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

Any plugin deregistering jQuery should just be rejected.

366 posts
  • Elite Author
  • Author had a File in an Envato Bundle
  • Most Wanted Bounty Winner
  • Exclusive Author
  • Microlancer Beta Tester
  • Attended a Community Meetup
  • Bought between 100 and 499 items
+4 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. ;)

2156 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 200 and 499 users
  • Has been a member for 5-6 years
  • Won a Competition
+10 more
mpc Volunteer moderator 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