Consider the fact that most WordPress installations are running more than just a theme. A WordPress installation is using a theme, yes, but in almost every case, it is also using plugins to extend the functionality of WordPress.
The wp_enqueue_script method takes several parameters:
- $in_footer – whether or not to include this file in the footer of your site instead of the header
The easiest way to explain this method is with an example, so let’s say we wanted to add jQuery to our WordPress theme. In this case, we only need to fill out one parameter since WordPress already includes the jQuery library in every installation
Where Do I Call wp_enqueue_script?
We place the wp_enqueue_script calls in a function, that we’re calling load_scripts. The name of this function isn’t important, but what is important is that we tie that function to the WordPress init action using the add_action call. This add_action call tells WordPress to call our function when it initializes. When load_scripts gets called it calls wp_enqueue_script to actually output the scripts to our page.
The cool thing about adding scripts this way is that WordPress is smart enough to make sure everything gets loaded up correctly. Scripts that are enqueued multiple times will only get output once, and with the use of dependencies, all the scripts will be output in the correct order.
So now you’ve seen the correct way to add scripts to your WordPress theme or plugin. Use it to make sure your scripts are deployed in the most reliable way possible. You can see a complete reference for wp_enqueue_script at the WordPress Codex