Loading JavaScript into WordPress Themes

Recently, I was working on a theme that was based on a modified version of TwentyTen, the default WP theme (www.corporate-chocolates.co.uk) and ran into an issue loading a jQuery file.

I had a .js file that ran a slideshow and some other functions and loaded it into my header.php file with this:
<script type=”text/javascript” src=”<?php bloginfo(‘template_url’); ?>/js/myfunctions.js”></script>

I noticed that the TwentyTen theme has an explicit note that wp_head() should always be placed just before the closing </head> tag or it will break many functions… Well, doing this means the javascript doesn’t load properly.

Solution? Put the call to the external file after the wp_head() line.

Incidentally, when loading jQuery into WordPress, it is better to use the full name rather than the $ alias as this prevents it conflicting with any other scripts that WP may be running.

other page


One Comment

It's good to talk!

Your email address will not be published. Required fields are marked *