How to Load JavaScripts into WordPress Themes

If you purchase through a link on our site, we may earn a commission. Learn more.

JQuery and other JavaScript frameworks are very commonly used in WordPress themes and plugins, but not all developers load them correctly. Learn how to properly load them using the wp_enqueue_script function in WordPress.
Table of Contents
WP Engine High Performance Hosting
BionicWP Hosting

JQuery and other JavaScript frameworks are very commonly used in WordPress themes and plugins, but not all developers load them correctly. You find many themes and plugins that load these frameworks directly using code like this:

[php]
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
[/php]

This is not the correct way to load scripts, as you might end up with more than one loaded version of JQuery, causing conflicts. WordPress has an inbuilt function called wp_enqueue_script which can be used to load scripts such as JQuery.

Using a straightforward piece of code as follows will load the JQuery library that ships with WordPress:

[php]
wp_enqueue_script( ‘jquery’ );
[/php]

This line could be placed in the theme’s header.php or functions.php file.

Note: If you’re loading directly into your header.php template file, make surethat the wp_enqueue_script function is above your wp_head function.Your custom jQuery code must go below the wp_head function. If you’re registering the wp_enqueue_script in the functions.php file, make sure that it comes before any custom functions that load throughthe add_filter function into the wp_head.

At this point the problem has been solved, but let’s go one better and load the JQuery framework hosted on Google’s CDN rather than the one which ships with your WP installation. Why would we do this? The CDN saves on bandwidth, allowing your site to do some parallel processing while downloading other scripts and collateral. Plus, it’s easy to always get the most current version of jQuery. jQuery’s library loads very quickly from Google’s CDN and, as a bonus, the library will already be cached if your site’s user has previously visited another site that delivers jQuery from Google Code’s CDN. To include jQuery from Google Code’s CDN, we’ll be sure to deregister jQuery then register through Google’s CDN. This is the beauty of registering and using the wp_enqueue_script function: if any other plugin or script requires jQuery, and doesn’t have any conflicts with the version loading up from Google, that script will use the already loaded Google CDN library. If a script depends on a specific version of jQuery, say 1.3.2 or 1.2.6, and the CDN is loading up version 1.4.2, then that script will go ahead and load the version of jQuery it requires. Because (as we’ll learn) every script loaded through the Script API stays in noConflict mode, it’s OK to have the two library versions loaded as long as they’re registered and required.

The code below (inserted into functions.php) deregisters the default JQuery library that comes with WordPress, and registers the CDN hosted version at Google. We also put in an if statement that makes sure Google CDN jQuery is not called on admin pages, as this may cause problems when editing posts, pages, or links.

[php]
function my_init_method() {
if (!is_admin()) {
wp_deregister_script( ‘jquery’ );
wp_register_script( ‘jquery’, ‘http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js’);
wp_enqueue_script( ‘jquery’ );
}
}

add_action(‘init’, ‘my_init_method’);
[/php]

Therefore the code directly above is all you need to insert in order to have JQuery loaded and available to use in your themes and plugins.

Jean Galea

Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Discover more from our archives ↓

Popular articles ↓

2 Responses

  1. Thanks Jean… I now use this method but I have an issue loading the jquery UI libary for some of my custom pages. The only way I was able make the UI work is loading the UI script in the header.php file outside of the WP_head(); . Any suggestions on why this is?

    1. Welcome, that is usually due to conflicts with some plugins. Not sure if there is a way of determining what loads first within wp_head(). Maybe ask on stackexchange and let us know if they suggest anything interesting.

Share Your Thoughts

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

Claim Your Free Website Tip 👇

Leave your name, email and website URL below to receive one actionable improvement tip tailored for your website within the next 24 hours.

"They identified areas for improvement that we had not previously considered." - Elliot

By providing your information, you'll also be subscribing to our weekly newsletter packed with exclusive content and insights. You can unsubscribe at any time with just one click.