Post Pic

How to include JQuery on your WordPress theme

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!

Related Posts

Related Posts

No related posts.

28 Responses

Jul 17 2009 14:38

First!

Amazing, all this time I’ve loaded it externally from Google, or self-hosted it. Thanks!

Jul 17 2009 15:08

That doesn’t work in here, I have WordPress 2.8.1.

Jul 17 2009 18:51

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);

Jul 17 2009 19:43

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!

Jul 18 2009 03:55

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

Jul 19 2009 03:17

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.

Jul 19 2009 07:22

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

Jul 20 2009 16:40

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!

Jul 20 2009 22:45

It’s a smarter choice to have your JQuery hosted at Google, speeds up your website.

Jul 22 2009 05:54

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?

Jul 24 2009 09:10

jQuery(document).ready(function($) {
// $() will work as an alias for jQuery() inside of this function
});

Jul 25 2009 02:37

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();

Jul 30 2009 20:49

Just tried in WordPress 2.8.2 and it doesn’t work. Will stick to pointing to google.

Aug 02 2009 09:37

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

Aug 22 2009 08:51

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?

Sep 11 2009 18:03

I love it!

The simple techniques are the best!

Sep 11 2009 19:05

Update: I decided to screw elegance… commented the original wordpress jquery line and hard-coded the script tags into my themes header file :)

Oct 06 2009 14:04

I try anything but i jquery does’t work in my theme.

Nov 25 2009 15:06

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)

Dec 21 2009 05:40

if doesn’t work
wp_enqueue_script("jquery");

you can try this
wp_print_scripts("jquery");

;)

Dec 21 2009 13:25

Please aware, you need to put this code above wp_head() to make it work

Dec 30 2009 01:14

Yeah, its cool! Very useful and cool thing!

Apr 16 2010 21:13

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

May 28 2010 17:46

I just hard-code direct link to jquery @ google code base. What is advantage of <?php wp_enqueue_script("jquery"); ?>

Jun 29 2010 07:16

for me , wp_enqueue_script(“jquery”); do not work at all !! !!
but wp_print_scripts(“jquery”); work carefully .

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required