1

I'm building a plugin that adds scripts to the page using wp_register_script and wp_enqueue_script. However, when the page loads, it's loading the plugin scripts before the theme's scripts. This is causing some errors because the theme is loading jQuery, which needs to be loaded first. Is there a way of making the plugin script load last, so that the theme script is loading first in the page?

2

For scritps and using the wp_register_script() function, you can define the dependeny of jQuery in the $deps argument, which is documented as:

$deps: Array of the handles of all the registered scripts that this script depends on, that is, the scripts that must be loaded before this script. These scripts will automatically be enqueued when this script is enqueued with wp_enqueue_script(). Set false if there are no dependencies. Default: array() http://codex.wordpress.org/Function_Reference/wp_register_script

Example:

wp_enqueue_script('custom-script', get_stylesheet_directory_uri() . '/js/my-jquery-custom.min.js', array('jquery'), '1.0', true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.