Using jQuery with WordPress

I struggled with this, as the usual method of including a javascript library and an external javascript script did not work.  Usually, I would do this:


<script style="text/javascript" src=<?php bloginfo('url'); ?>/wp-includes/js/jquery/jquery.js" ></style>
<script style="text/javascript" src=<?php bloginfo('template_directory'); ?>/myScript.js" ></style>

But that didn’t work at all, even when I included the jQuery.noConflict() line in my script. So I ended up going with this, which loads the jQuery library that is included with the wordpress installation and then loads my script without any conflicts:


<?php wp_enqueue_script( 'myScript', '/wp-content/themes/myTheme/myScript.js', array('jquery') ); ?>

The wordpress docs have very little to say about wp_enqueue_script(), the function that makes this possible. A good resource with examples of use can be found here:

http://nickohrn.com/loading-javascript-libraries-in-wordpress-plugins-with-wp_enqueue_script/

2 Responses to “Using jQuery with WordPress”

  1. Nick Says:

    Thanks for the link and I’m glad you were able to find my article useful. You should always try to use wp_enqueue_script to prevent conflicts when you’re including JavaScript.

    What plugin are you working on?

  2. likesalmon Says:

    I’m not working on a plugin per-se, I’m doing some addClass() and sIFR magic for a new site I’m developing. I might abandon sIFR if I find a good enough plugin from someone else.

Leave a Reply