6

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.

  • IIRC this is normally done by its registered name. Please show some code that demonstrates your issue and also what you've tried so far and how you've got tested that it does not work. – hakre May 25 '13 at 12:37
  • @hakre I'm asking how to add a script as a dependency to another script which is registered by another plug-in. Not sure that requires me to post accompanying code. I'm guessing attacking the global $wp_scripts is the only thing that can be done here - but I wanted to if there were other/better ways. – Stephen Harris May 25 '13 at 13:42
  • Wasn't meant as a requirement, just for adding more context to your question. See as well what @Rarst has answered. – hakre May 25 '13 at 13:47
12

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:

/**
 * Add $dep (script handle) to the array of dependencies for $handle
 * @see http://wordpress.stackexchange.com/questions/100709/add-a-script-as-a-dependency-to-a-registered-script
 * @param string $handle Script handle for which you want to add a dependency
 * @param string $dep Script handle - the dependency you wish to add
 */
function wpse100709_append_dependency( $handle, $dep ){
    global $wp_scripts;

    $script = $wp_scripts->query( $handle, 'registered' );
    if( !$script )
        return false;

    if( !in_array( $dep, $script->deps ) ){
        $script->deps[] = $dep;
    }

    return true;
}

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 the init hook with priority 10, and you want to add script_b as a dependency:

add_action( 'init', 'wpse100709_register_script_b', 11 );
function wpse100709_register_script_b(){

   //Register script b
   wp_register_script( 'script_b', ... );

   //Now add script b as being a pre-requisite for script a
   wpse100709_append_dependency( 'script_a', 'script_b' )

   //If script a is enqueued, script b is enqueued before it.

}
| improve this answer | |
  • Not to criticize, but I am having a hard time working out why this would ever need to be done. Do you mind explaining? What is the circumstance that prompted this? – s_ha_dum May 25 '13 at 14:38
  • I have a javascript file in one plug-in, and another in an add-on to this pug-in. I want to load the latter when the former is loaded. Of course there are other ways round this - but for maintainability I do not want to edit the original plug-in (might not always be possible anyway). – Stephen Harris May 25 '13 at 15:48
  • @s_ha_dum More generally I was thinking of how an add-on could modify the behaviour of a plug-in in a JS-context, with view of: github.com/carldanley/WP-JS-Hooks – Stephen Harris May 25 '13 at 15:53
  • 1
    I needed this to fix a problem where different plugins were adding scripts, but they needed to be in a certain order to work. Adding one as a dependency of another ensured that they were loaded in the correct order. And I didn't have to edit any of the plugins, so it's update-proof! – J.D. Nov 18 '13 at 20:07
4

There is no designated way to change details of registered script/style dependency after the registration. Your options are:

  • deregister it and register again with desired data
  • modify data in corresponding global variable
  • handle timing in other ways, such as manual output and hook priorities
| improve this answer | |
  • +1. 1 or 2 are, in my opinion, more portable solutions. 1st solution doesn't (quite) require any less touching of globals because you need to retrieve previous data to merge it with the new (in this case an added dependency). I've implemented 2 above. – Stephen Harris May 25 '13 at 14:24

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.