WordPress : Using jQuery library with your plugin or theme

When you develop a WordPress plugin or theme, you might want to make use of the jQuery library included and distributed with the WordPress package.

The jQuery library is located inside the “wp-includes/js/jquery/” folder. Even though you know what the location of the jQuery library is, I do not recommend that you reference this script using a <script> tag. Rather make use of the wp_enqueue_script function included in the WordPress core which allows you to safely reference your Javascript scripts. I recently wrote an article entitled Using Javascript libraries with your WordPress plugin or theme which explains the wp_enqueue_script function.

So, first off, fire the wp_enqueue_script function in order to safely reference your jQuery library to ensure that the jQuery core is available for usage with your plugin or theme. Other plugins might fire this function with the same handle in order to use jQuery, but you don’t know this and you aren’t sure what other plugins your users will be using, so you have to be sure that it is included.

<?php wp_enqueue_script(‘jquery’); ?>

Ultimately, WordPress will insert a <script> tag into the <head> section of the page referencing the jQuery library by including the “jquery.js” source file.

Next thing to do is prevent conflicts between libraries. By default, jQuery is used with a global variable which is a dollar sign. See the example below which shows (makes visible) an element on the page with the ID “mydiv”.

$("#mydiv").show();

Unfortunately, it is expected that this will conflict with other libraries which are also included and distributed with WordPress such as Prototype and Scriptaculous. WordPress (2.5+) don’t use these libraries on the front end or in the administration panel, though other third party plugins or the current theme which you are using might make use of these libraries which will result in a conflict between the libraries, causing a Javascript error on the page and terminating the execution of all other Javascript on the page.

A great workaround for this is to make use of the jQuery noConflict() method which allows you to create a new global Javascript object which is practically a jQuery instance on its own. See the example code below which shows you how to create this new, global object.

<script type="text/javascript">
$myjQuery = jQuery.noConflict();
</script>

With that done, you can now use this object instead and be confident that there won’t be any conflicts between the jQuery library and other libraries inside WordPress. Below is the modified code for making the element with the ID “mydiv” visible as shown earlier.

$myjQuery("#mydiv").show();

I hope this helps! Best of luck and feel free to post your comments!

, , , , ,

This post was written by:

Antonie Potgieter - who has written 57 posts on Lost-In-Code.

I (Antonie Potgieter) am a software engineer/web developer located in South Africa. My full-time work is the management of Tribulant Software and the development of its software packages.

Contact the author

6 Comments For This Post

  1. 生活健康网 Says:

    不错不错WordPress : Using jQuery library with your plugin or theme

  2. Ian Villanueva Says:

    what could be the problem with this “”

    I’ve also tried using for me to define the exact location of the jquery.js but still doesn’t work for me. any comment?

  3. Mitch Says:

    Im having a jquery conflict on my feature content glider! The glider jquery is conflicting with photosmash galleries and lightbox-pro in wordpress.mu root page.

    I tried the tutorial but not sure where to place the code, I have placed it in the header.php but it hasnr worked yet!

    can you help me?

  4. Antonie Potgieter Says:

    Im having a jquery conflict on my feature content glider! The glider jquery is conflicting with photosmash galleries and lightbox-pro in wordpress.mu root page.

    I tried the tutorial but not sure where to place the code, I have placed it in the header.php but it hasnr worked yet!

    can you help me?

    @Mitch,

    You can try the WordPress slideshow gallery plugin which I recently developed and released.

  5. Mitch Says:

    Nice, can I hook it up with the 3 slide tabs on the top root.
    Check it out at Thaub.com. Contact me through that if you can help.

    Thanks

  6. GT Says:

    Oops – I mis-wrote.

    The main page of my site does get an ‘A’ from YSlow… it is the pages that contain a little more ‘behaviour’ that get a B (score of 86).

4 Trackbacks For This Post

  1. Eccentric 離心 » Blog Archive » [WP] wordpress開發誌3-後台的Jquery衝突 Says:

    [...] 就是把原本的$用$j取代,這是在support看到的作法,也可參考Wordpress : Using jQuery library with your plugin or theme。 Filed under [wp] 只緣身在此山中 having [...]

  2. Eccentric 離心 » Blog Archive » [wp] wordpress 開發誌-使用jQuery Says:

    [...] 就是把原本的$用$j取代,這是在support看到的作法,也可參考Wordpress : Using jQuery library with your plugin or theme。 Filed under [wp] 只緣身在此山中 having [...]

  3. FBN Testing Site » Blog Archive » Featured 1 Says:

    [...] WordPress : Using jQuery library with your plugin or theme Eccentric ?? » Blog Archive » [WP] wordpress???3-???Jquery?? Says: … Turn offerror reporting on your production site · WordPress : Banner Ad … http://www.lost-in-code.com/platforms/wordpress/wordpress-using-jquery-library-with-your-plugin-or-theme/ – 42k - Cached - Similar pages [...]

  4. Plugin | jquery | theme | jquery conflict | wordpress | ajax uploading | problem | help | Fix | Another Wordpress Classifieds Plugin Says:

    [...] am trying out the fix suggested in WordPress : Using jQuery library with your plugin or theme. Will update on my progress [...]

Leave a Reply