A better way to use jQuery in WordPress Themes

Since a lot of people come to my blog looking for a way to add jQuery into their wordpress themes, it turns out that there is a better way to do it than the way that I previously blogged. You can include jQuery from the current WordPress install by calling:

wp_enqueue_script("jquery");

Followed by:

wp_head();

You can even use it to include the jQuery library that you are adding with your theme (so that it works after wordpress updates their library):

//First you have to get rid of the one that wordpress includes:
wp_deregister_script('jquery');
//Then you have to replace it with your own:
wp_register_script('jquery', (bloginfo("template_url")."/js/jquery.min-1.3.2.js"), false, '1.3.2');
//or you could use the one that google so kindly provides:
wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"), false, '1.3.2'); 
//Then enqueue it
wp_enqueue_script('jquery');


Related posts:

  1. JQuery and WordPress
  2. A couple useful jQuery Snippets
  3. Updated WordPress
  4. Using the jQuery-UI Autocomplete Widget
  5. Ajaxy Pagination using jQuery

Tags: , , , , , , , ,

  • http://maltaannon.com/ Maltaannon

    Ok. I’ve solved it after all. :) Was tricky since it had to be integrated with few other things as well. Gotta love jQuery ;)

    Keep up the good work with your site. Will come back for more tips. Cheers

  • http://maltaannon.com/ Maltaannon

    Hey. This is exactly what I was looking for. Pretty new to jQuery, although some programming background. Maybe you can help me. I’ve been looking for something on the net with no results. I’m trying to integrate some ad system to my JW Player (player for flash movies) that is included in the Shadowbox.

    I need to wrap “sb-body-inner” with some provided div. Tried wrapInner – gave me “is not a function” error (thats why i thought it would be best to replace wp’s jquery with the latest one).

    Then I replaced $ with jQuery. I get no error, but no results either.

    Plus – I need to add params to the to the JW Player. Shadowbox docs says that it can handle that, but it seems it doesn’t work. A lot of complains on the forums about it.

    Above that theres a huge problem – shadowbox creates new DOM elements, so I can’t just add them when the page loads. I need to add them after they are created – in other words I need to add some kind of event handler, or call another function after clicking on the link that produces the shadowbox.

    Phew… so… can I count on you? ;)