
JQuery is definitely a great Javascript framework, which allow you to, among other things, create stunning visual effects. Including JQuery is easy. But do you know that WordPress can make it even easier?

JQuery is definitely a great Javascript framework, which allow you to, among other things, create stunning visual effects. Including JQuery is easy. But do you know that WordPress can make it even easier?
The first thing you have to know is that WordPress already have JQuery (It is used in the dashboard) so you don't need to download a new copy of JQuery to your WordPress server.
Then, we just have to use the wp_enqueue_script() function, which automatically enqueue a Javascript file in your theme header.
Open your header.php file and paste the following within the <head> and </head> tags:
<?php wp_enqueue_script("jquery"); ?>
By the way, Cristian just wrote a very interesting post at Cats Who Code. It is all about WordPress Theme Frameworks so be sure to check it out!
Thanks to Chris Coyier for the tip!
28 Responses
First!
Amazing, all this time I’ve loaded it externally from Google, or self-hosted it. Thanks!
That doesn’t work in here, I have WordPress 2.8.1.
One thing worth taking note of is the conflict with prototype which may cause it to not work in IE at least. Be sure to call the noConflict() method.
$j = jQuery.noConflict();And you can call any jQuery functions like such:
$j(this).fadeTo("normal", 0.5);If you call jQuery from Google (see below) you can be assured you’re always using the most up-to-date version. Otherwise, the version included inside WP may be dated. (Purposely removed start/end code to post)
script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js”>
If you don’t care so much about being *exactly* current, the above code is great!
Thanks. I didn’t know about this. I used to call jQuery from lib, or download them to my server and enqueue it internaly. Top banget deh
Please be aware that you need to put the wp_enqueue_script function call above the wp_head function call for this method to work.
I already wrote a blog about this. In that I demonstrate how to integrate a thickbox in WordPress. Have a look
http://www.myhtmlworld.com/wordpress/create-thickbox-wordpress.html
It didn’t work for me either. Already placed it inside tag and above wp_head function. Maybe it’s a version issue. I’m also using 2.8.1 (wpmu).
I’ll try it on single user later.
Anywayz, I really learned a lot from your recipes. Nice job!
It’s a smarter choice to have your JQuery hosted at Google, speeds up your website.
Why this code doesn’t work on me too?
I was follow Nick Ohrn tips by adding this code above wp_head()
but the code didn’t show up
Any suggestion?
jQuery(document).ready(function($) {
// $() will work as an alias for jQuery() inside of this function
});
Just an FYI… for those that link directly to Google, WordPress’ version of jQuery is different.
At the bottom of it, WP adds the following line:
jQuery.noConflict();
Just tried in WordPress 2.8.2 and it doesn’t work. Will stick to pointing to google.
It’s a great tip for theme AND plugin development. In general it’s much more clean and elegant to call scripts by queueing them. One of the most important benefits is cross script compatibility. And while we are at wp_enqueue could you please also write about wp_localize_script – wp_enqueue’s “brother in law”?
I’ve tried everything and then some and still no luck with manually pointing to Google’s CDN. I deregistered jquery and manually inserted the script tags in my template’s header and it loads but nothing works. I also created a script tags immediately after with jQuery.noConflict() but that seemed to make no difference… oh man! Any suggestions?
I love it!
The simple techniques are the best!
Update: I decided to screw elegance… commented the original wordpress jquery line and hard-coded the script tags into my themes header file
I try anything but i jquery does’t work in my theme.
Hi,
I had the same problem on one site.
Normally jquery is included by plugins, but than you need to include the function “” in your footer template
(at least that solved my problem)
if doesn’t work
wp_enqueue_script("jquery");you can try this
wp_print_scripts("jquery");Please aware, you need to put this code above wp_head() to make it work
Yeah, its cool! Very useful and cool thing!
@jauhari, you’re right guy, it must to put this code above wp_head() to make it work. I’ve tried before, but it didn’t work.
Thanks.
I just hard-code direct link to jquery @ google code base. What is advantage of
<?php wp_enqueue_script("jquery"); ?>for me , wp_enqueue_script(“jquery”); do not work at all !! !!
but wp_print_scripts(“jquery”); work carefully .
Trackbacks: