2210 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?

348 posts
  • Won a Most Wanted contest
  • Won a competition
  • Located in Netherlands
  • Has sold $40,000+ on Envato Market
+6 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' );
2210 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?

348 posts
  • Won a Most Wanted contest
  • Won a competition
  • Located in Netherlands
  • Has sold $40,000+ on Envato Market
+6 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 :)

2210 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! :)

488 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.

3503 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.

2010 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.

376 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Sells items exclusively on Envato Market
  • Has sold $500,000+ on Envato Market
  • Made it to the Authors' Hall of Fame
+8 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. ;)

2210 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?

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by