FTOB: Using jQuery with Wordpress

Originally posted on 10/10/08 at http://likesalmon.wordpress.com

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/

This entry was posted in From the old blog, Tutorial, Wordpress. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>