2

Consider this scenario:

I enqueued some scripts in my custom theme. Then I set off to building a plugin that I want to be used only with that one theme. I want to enqueue other scripts in my plugin that depend on scripts enqueued in the theme. The standard way of declaring script dependencies in wordpress is passing an array of dependencies to wp_enqueue_script. E.g.:

wp_enqueue_script('tagsinput-scripts', plugin_dir_url( __FILE__ ) . 'js/tagsinput/bootstrap-tagsinput.min.js', array('bootstrap-js'), '130215', false);

would not be enqueued until bootstrap-js is enqueued.

Imagine that the script above was enqueued by my plugin with admin_enqueue_scripts action hook, while bootstrap-js was enqueued in the theme with the wp_enqueue_scripts action hook.

Can the dependency between tagsinput-scripts and bootstrap-js be properly resolved by Wordpress in this scenario when the two scripts where enqueued using different action hooks?

0

The simple answer is, "No". Although admin_enqueue_scripts and wp_enqueue_scripts hooks does exactly the same thing, the do their work in separate places which do not have any reference to the other.

  • wp_enqueue_scripts runs on the public side or front end

  • admin_enqueue_scripts As the name suggests, it runs on the admin side or back end

Your script that with the dependency will thus never load if it is depended on a script that is loaded via the other hook.

You will have to hook your bootstrap-js to both hooks for this to work, this will ensure that your dependency will work as well

Example:

add_action( 'wp_enqueue_scripts', 'enqueue_bootstrap_js' );
add_action( 'admin_enqueue_scripts', 'enqueue_bootstrap_js' );

function enqueue_bootstrap_js()
{
    //Register and enqueue your bootstrap-js script
}

add_action( 'wp_enqueue_scripts', 'tagsinput_scripts' );

function tagsinput_scripts()
{
    wp_enqueue_script('tagsinput-scripts', plugin_dir_url( __FILE__ ) . 'js/tagsinput/bootstrap-tagsinput.min.js', array('bootstrap-js'), '130215', false);
}
  • Thnak you for this detailed reply. It helped me a lot. – luqo33 Mar 17 '15 at 8:56
  • My pleasure. Enjoy :-) – Pieter Goosen Mar 17 '15 at 8:57
1

The simple answer is "Yes".

Using wp_enqueue_scripts or admin_enqueue_scripts does make the script a dependency however it will only be recallable in the local environment. Thus only in the theme or plugin where you are registering it. However what what Pieter Goosen nor WordPress doesn't tell you is that you can use the wp_default_scripts hook to register an installation wide script.

How to use? See the note below, what's currently awaiting moderation for the "User Contributed Notes" of the wp_default_scripts action hook at the WordPress Developer Resources.


Add a script where you can refer to from anywhere in your WordPress installation using wp_enqueue_script or admin_enqueue_script.

First setup the hook, for example in your plugin.

add_action('wp_default_scripts', function(&$scripts) {
  // Arguments order: `$scripts->add( 'handle', 'url', 'dependencies', 'version', 'in_footer' );`
  // Set "in_footer" argument to 1 to output the script in the footer.
  $scripts->add('my-plugin-script', '/path/to/my-plugin-script.js', array('jquery'), 'alpha', 1);
});

Now you can use the script from the plugin anywhere for example in your theme.

add_action('wp_enqueue_scripts', function () {
  // Note the third argument: `array('my-plugin-script')`
  wp_enqueue_script('my-theme-script', '/path/to/my-theme-script.js', array('my-plugin-script'), null, true);
});

Please make sure that you pick an identical string for the "handle" argument, a good practice is prefixing the handle name with your plugin or theme name.

  • This is possibly the only place on the internet that has this answer! I've been trying to solve this problem for many hours and this seems like a very promising solution. – Eckstein Jan 6 '18 at 4:06

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.