0

I am developing a theme as well as a few plugins that go along with it. I'm having trouble figuring out dependencies of enqueued files between a theme and a function. If I enqueue a custom .js file in my theme, and then make that file as a dependency of a file I enqueue in my plugin, should I expect the functions from the theme .js file to work in the plugin .js file?

In theme functions.php:

wp_register_script( 'theme-script', 'path/to/theme/script.js', array('jquery'));    
wp_enqueue_script( 'theme-script');

In plugin my-plugin-name.php:

wp_enqueue_script( 'plugin-script', 'path/to/plugin/script.js', array('theme-script'));

Is this the way I should do this in order to use functions from the theme .js file in my plugin?

1

Yes. Dependencies work across all of WP, it doesn't matter where you enqueue your script. All that matters is that you define your dependencies correctly, and you're doing that.

If, however, you plan to publish both the theme and the plugin as standalone versions, you shouldn't rely on your plugin being used with your theme, and add the same script to your plugin (using the same name so it'll only be loaded once by WP). If theme and plugin are only available as a combo, don't worry about it.

  • Thank you, that clears it up. I was hesitant to include the script in both, but I'm glad to hear it will only enqueue once with the same name. – Eckstein Jan 6 '18 at 15:55

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.