Use js script from one plugin in another plugin

The only thing that determines whether a function is available in another script is if it has been loaded before the other script attempts to use it. This is simply a matter of putting the <script></script> tag for the script with the function you need to use before the tag of the script that needs to use it*.

The most reliable way to guarantee that the script you depend on has loaded before the script that needs it is to use the dependencies argument of wp_enqueue_script():

wp_enqueue_script( 'dependency', '/path/to/dependency.js' );
wp_enqueue_script( 'script', '/path/to/script.js', array( 'dependency' ) );

The 3rd argument is an array of handles (the first argument) for scripts that the script depends on. This ensures that 'dependency' will be loaded whenever 'script' is enqueued, and before it.

*Unless you’re doing something unusual like loading scripts asynchronously, which WordPress doesn’t support with wp_enqueue_script() out of the box.

Leave a Comment