Adding Javascript To WordPress Themes The Right Way

When I discovered the power of JavaScript I’ve been using it since to enhance my WordPress themes with image sliders, hover animations, drop-down menus…etc.

There is a specific way for adding Javascript to your WordPress theme in order to prevent any conflicts with plugins, the problem is that many theme developers (yes I did this mistake when I was first learning to code WordPress themes) call their javascript files directly in the header.php file instead of the functions.php file.

In this blog post I will show you how to correctly call your javascript files using your functions.php file so they load right into your wp_head tag and also a quick trick for using the Google hosted javascript instead of using the Javascript included with WordPress then the one in the WordPress install.

Wrong Way To Add Javascript To WordPress Themes

Calling the javascript in your header.php file, such as shown below, is NOT the best way and I highly recommend against it because it often times causes conflicts with other plugins that load their own JS scripts.

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%20src%3D%22%2Fscripts%2Fmyscript.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />

The Right Way To Add Javascript To WordPress Themes

The better for adding javascript to your WordPress theme is to do so via the functions.php file using wp_enqueue_script. Using an action to load your javascript will help keep your theme out of trouble.


<?php wp_enqueue_script( 'myscript', get_template_directory_uri() . '/js/myscript.js'); ?><br />

The code above will add the code needed to load the myscript.js file into the header.php file at the wp_head hook. As you can see I have only included the $handle, everything else is technically optional but probably a good idea to add to make it easier for people editing the theme.

The wp_enqueue_script() function can technically be used in any template file but it’s best to place all your scripts into a single function (like the example below) using the enqueue hook. This will keep things much neater and easier to edit in the future as well as easier to debug issues.

Using The WordPress Enqueue Hook

Fortunately WordPress has two different action hooks you can use for calling your scripts.

1. wp_enqueue_scripts – action used to load scripts on the front-end

2.  admin_enqueue_scripts – actionused to load scripts in the WP admin


Here is an example (that would be added to your functions.php file) of how to create a function and then use the WordPress hook to call your scripts.

function my_theme_scripts_function() {<br />
   wp_enqueue_script( 'myscript', get_template_directory_uri() . '/js/myscript.js');<br />
}<br />
add_action('wp_enqueue_scripts','my_theme_scripts_function');<br />
  • Published on: April 14, 2011
  • Last Updated on:
  • Posted Under: WordPress Tutorials
AJ Clarke
Post Author:

Hey. My name is AJ and I am the boss around here. I own and operate WPExplorer. A website dedicated to everything I love about WordPress.

Disclosure: This page contains external affiliate links that may result in us receiving a comission if you choose to purchase said product. The opinions on this page are our own. We do not receive payment for positive reviews.
Got something to say? Join the discussion.
    • AJ Clarke | WPExplorer says:
      AJ Clarke | WPExplorer

      In functions.php is fine. But also now in WordPress you can enqueue a script on any file without having too hook into wp_enqueue_scripts

  1. Alan says:

    Do you have to add this code for every file? Is there a way to add multiple files without having to call a function for each one?

    • AJ Clarke | WPExplorer says:
      AJ Clarke | WPExplorer

      You have to use wp_enqueue_script() for each file you want to load, but you can place them all into 1 function and hook into “wp_enqueue_scripts”. If you really want I guess you could create an array of the file names you want to load and if they are all in the same location do a foreach loop to go through each of the items in the array so you only use wp_enqueue_script() once.

  2. Dan says:

    Hi AJ,

    For all of us noobs out there, can you detail (and visually illustrate) where “The Action” and the “Example” (both) would be placed? Maybe even break it down to show how the functions file would look after you have added the script?


    • AJ Clarke says:
      AJ Clarke

      Hi Dan,

      I just updated the post a bit, but have a look at the CODEX it explains it much better. You can use the wp_enqueue_script function technically anywhere, however, it’s much cleaner to keep it in your functions.php file or a file that is called from the functions.php, because having all the scripts loaded from 1 place makes it easier to troubleshoot, debug and update.


Leave a Reply