jQuery in WordPress: wp_enqueue_script

Did anybody notice the new sitemap I did for the blog? Top-right, in the black part. Click on sitemap. Smooth! For those of you who don’t know, that’s jQuery. Okay, seriously, this is my first ever jQuery script because I love Prototype. I don’t know why I did that, since I could have done it with Scriptaculous. So, about scripts in WordPress… You all heard about the function wp_enqueue_script in WordPress, yeah it’s cool, but what if I want to use both Prototype AND jQuery? Yeah, sounds stupid, but what about having two different plugins installed. One that uses Prototype, the second one jQuery. Compatibility?

Took me quite some time to figure out how to use jQuery $() function in WordPress though. I really think that the man who came up with this is a genius. There’s a synonym for the $() function in jQuery called jQuery(). WordPress’ers must have removed the $() function for Prototype compatibility.

jQuery(document).ready(function(){
	jQuery(".sitemap-click").click(function(){
		jQuery("#sitemap").fadeIn("slow");
		return false;
	});
});

So instead of $() we use jQuery(). Sweet.

Oh and I’m also glad that Skype on iPhone will be free and official!

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

10 comments

  • Hey! Great tip! Your sitemap action would be even *smoother* if the link was actually a toggle, so the sitemap could could be dismissed too.

    Your code could be easily modified to do this:
    <code>
    jQuery(document).ready(function(){
    jQuery(".sitemap-click").toggle(function(){
    jQuery("#sitemap").fadeIn("slow");
    return false;
    },
    function(){
    jQuery("#sitemap").fadeOut("slow");
    return false;});
    });
    </code>

    Give'r a go!

  • I ran into a strange issue today. I wrote a plugin for jQuery that used the load events from jQuery attached to an image. This plugin worked perfectly in Firefox, Safari, and Opera on PC, Mac, and Ubuntu. The only browser that it had problems with was IE.