What is the point of the dependencies parameter when using wp_enqueue_script()?
For instance, when I do the following:
wp_enqueue_script(
'jscripts',
get_stylesheet_directory_uri() . "/scripts/jscripts.js",
array('jquery-ui-datepicker'));
echo wp_script_is('jquery-ui-datepicker', 'queue')
? '<br>' . '[jquery-ui-datepicker] Script is enqueued' . '<br>'
: '<br>' . '[jquery-ui-datepicker] Script not enqueued' . '<br>';
I get the output ‘Script not enqueued’ (and the function does not work properly). However, when I do this:
wp_enqueue_script(
'jscripts',
get_stylesheet_directory_uri() . "/scripts/jscripts.js");
wp_enqueue_script('jquery-ui-datepicker');
echo wp_script_is('jquery-ui-datepicker', 'queue')
? '<br>' . '[jquery-ui-datepicker] Script is enqueued' . '<br>'
: '<br>' . '[jquery-ui-datepicker] Script not enqueued' . '<br>';
It works properly and it displays ‘script is enqueued’.
Having read the documentation, it would seem like when you specify the handle of dependencies for a script that you are about to enqueue, it will enqueue those dependencies before enqueing it. But, this doesn’t seem to be the case. How does this all work?
Try this. You have to call wp_enqueue_script at the appropriate time, so that it can queue your scripts until the dependencies you specify are called –
This is assuming your scripts are to be called on the front end. Substitute
wp_enqueue_scripts
withadmin_enqueue_scripts
if it is for the admin area (for a plugin, for example).Please use the files & hooks that are meant to be used:
wp_enqueue_scripts
public/templates/themesadmin_enqueue_scripts
Admin UIlogin_enqueue_scripts
Then wrap everything in a function and hook it from your
functions.php
file: