It’s difficult if you do not understand some of the terminology. “Callback”, noun – a term used by coders for a function that is passed as an argument to another process. This argument will then be called (back) at the appropriate time during that other process’ execution. A common use of callbacks is in JavaScript when an event “listener” as added to a particular element on a webpage, such as a mouse click on a submit button. When that listener is added, a callback is passed as an argument. When that mouse click event happens (or “fires”), the callback function is called. Typically the callback function does something necessary related to the event that fired. That’s a quick example of a callback.
The setup for enqueuing scripts is much more involved. To really understand it, you need to understand in general how action and filter hooks in WP work. It is not easy to grasp at first, but once you do, you will find it’s really rather simple. I don’t think I can adequately explain it, though. I tried to find a good reference, but was not satisfied with what I found. Maybe this article will help. While not satisfactory to me, it’s better than most.
In the case of enqueuing scripts with wp_enqueue_script(), this function is required to be within a callback for the action “wp_enqueue_scripts”. Devs would normally add such callbacks (using add_action()) for a plugin or theme. By calling add_action() in plugin or theme code, WP stores the passed callback function in an array keyed by the action tag (“wp_enqueue_scripts” in our case). When themes and plugins are loaded for WP, WP is still in an unstable state. Once WP is at a place where it can properly handle calls to wp_enqueue_script(), if fires the “wp_enqueue_scripts” action by calling do_action('wp_enqueue_scripts');
The do_action() function retrieves all functions stored in that array under the key “wp_enqueue_scripts” and calls each function listed there (sometimes referred to as an action “stack”) in turn by calling call_user_func() for each saved function. These callbacks for “wp_enqueue_scripts” could do any number of things, but generally call wp_enqueue_script() one or more times.
That covers your part of the process. What WP does with the information is rather convoluted. Understanding what happens is helpful for deeper understanding, but many choose to simply take it on faith that it works, that the enqueued scripts are output to the web page in the necessary order. For anyone interested, here is what happens.
What wp_enqueue_script() does is it saves the passed information in yet another array. After all callbacks for “wp_enqueue_scripts” are called and all scripts are saved into that array, WP processes the information, creating a list of HTML script tags in a particular order based on the listed dependencies. Scripts that many others are dependent on, but themselves have no dependencies will be listed first. These are typically libraries like jQuery or the maps API. Scripts that have multiple or no dependencies are generally listed last. Several registered libraries that are included with the WP distro, mostly related to jQuery, are all handled by a single script tag that references /wp-admin/load-scripts.php, followed by a list of registered handles that need to be loaded for the current web page. All other scripts will have their own script tag for each file.
When a web page is output, there should always be a call to wp_head() on one of the page’s template files. This generally occurs in the header.php file of every theme. The call must be made before the losing HTML head tag of the page. All the wp_head() function does is call do_action('wp_head');
By firing the ‘wp_head’ action, all sorts of preliminary callbacks are executed that set things up for page output. One of these callbacks, added by WP itself internally, is wp_print_scripts(). This function takes the list of script tags previously established and outputs them. This causes the browser to retrieve the related files as part of the page loading process.
Whew! Aren’t you glad you don’t have to manage all of that? To quickly review, here is an outline of the enqueuing process:
Theme or plugin adds callback to “wp_enqueue_scripts”
That callback in part calls wp_enqueue_script(), your part is done.
————
After WP loads, it fires the “wp_enqueue_scripts” action
Callbacks that were added execute and call wp_enqueue_script()
wp_enqueue_script() adds script data to an array.
After the “wp_enqueue_scripts” action completes, WP works out the dependencies
When the page loads, wp_head() is called, firing “wp_head” action
One callback is wp_print_scripts(), which outputs script tags in the correct order
I hope this helps. I’m sorry it’s so long. I fear it may only serve to confuse things. All you really need to know is that you enqueue scripts from a callback to “wp_enqueue_scripts” (admin and login areas have their own hooks). The rest provides a deeper understanding, but fully understanding it is not required. Just trust that it works.