- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Community Moderator
- Author was Featured
- Bought between 10 and 49 items
- Referred between 200 and 499 users
- Has been a member for 4-5 years
- Won a Competition
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?
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' );
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Community Moderator
- Author was Featured
- Bought between 10 and 49 items
- Referred between 200 and 499 users
- Has been a member for 4-5 years
- Won a Competition
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?
<?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 
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Community Moderator
- Author was Featured
- Bought between 10 and 49 items
- Referred between 200 and 499 users
- Has been a member for 4-5 years
- Won a Competition
Thanks! 
- Bought between 10 and 49 items
- Contributed a Blog Post
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 4-5 years
- Item was Featured
- Microlancer Beta Tester
- Referred between 100 and 199 users
- Sold between 10 000 and 50 000 dollars
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.
- Sold between 250 000 and 1 000 000 dollars
- Community Moderator
- Author was Featured
- Item was Featured
- Bought between 50 and 99 items
- Referred between 1000 and 1999 users
- Has been a member for 3-4 years
- Repeatedly Helped protect Envato Marketplaces against copyright violations
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.
- Sold between 100 000 and 250 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
- Author was Featured
- Item was Featured
- Bought between 10 and 49 items
Do not mess with jquery and just use the one bundled with wp.
Any plugin deregistering jQuery should just be rejected.
- Author was Featured
- Item was Featured
- Author had a File in an Envato Bundle
- Microlancer Beta Tester
- Exclusive Author
- Most Wanted Bounty Winner
- Attended a Community Meetup
- Bought between 100 and 499 items
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. 
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Community Moderator
- Author was Featured
- Bought between 10 and 49 items
- Referred between 200 and 499 users
- Has been a member for 4-5 years
- Won a Competition
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?
