Blog

Blog Post

Loading custom Javascript Libraries in Wordpress Themes with wp_enqueue_script()

Since reading Nick’s entry on Loading Javascript Libraries in Wordpress Plugins, I have decided to use the same techniques to get my custom Theme to load JavaScript using wp_enqueue_script().

There is two reasons why I wanted to load my custom JavaScript libraries:

  1. Default jQuery library included with the latest version of WordPress is not up-to-date
  2. My custom code does not work with the default version included with this release WordPress

As this is a custom theme I have a functions.php where I have defined all my custom functions. So to load the custom JavaScript.

< ? php
function my_add_js_libs() {
//Add the jquery framework to the header
//wp_enqueue_script('jquery'); // default jquery is not up-to-date
//wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false );
wp_enqueue_script('my_jquery', $src = '/wp-content/themes/[mythemename]/js/jquery-1.2.1.pack.js’, $deps = array(), $ver = ‘1.2.1′ );
wp_enqueue_script(’my_core_js’, $src = ‘/wp-content/themes/[mythemename]/js/core.js’, $deps = array(’my_jquery’));
}
? >

then make a call to the function in the header.php just before the wp_head();

< ? php
my_add_js_libs();
wp_head();
? >

This should now load the custom JavaScript Libraries required for your themes in the sequence that WordPress decides.

No comments yet! Be the first person to leave a comment.

Leave a Reply