wp_register_script()
(see codex) allows you to specify dependencies: scripts that must be loaded before the one being registered is loaded (if it is loaded).
But suppose the script is a third-party one (WordPress or another plug-in) so that you are not the one calling wp_register_script()
. How can you inject a script as a dependency for a pre-registered script?
Remarks: In my particular use-case the dependency is not strict – the registered script does not require this second script, but the latter alters the original script.
A similar question could be asked for styles, but I suspect the answers would be nearly identical.
Digging through https://github.com/WordPress/WordPress/blob/3.5.1/wp-includes/class.wp-dependencies.php all registered scripts are stored in the global
$wp_scripts
.You can access them directly through that, but I prefer to use the API when it exists. In this case,
$wp_scripts->query()
returns a particular registered script (a_WP_Dependency
object – see source).A
_WP_Dependency
object stores the dependencies as an array of handles, which you can access directly, and insert a dependency. The following function does that:Obviously you must add this somewhere between the original script (
$handle
) being registered and it being enqueued.Example usage
Suppose
script_a
has been registered on theinit
hook with priority 10, and you want to addscript_b
as a dependency:There is no designated way to change details of registered script/style dependency after the registration. Your options are: