Combine enqueue js without affecting dependencies

From your comment above I read

This is one of the reason why i am looking for a way to say to
wordpress: “hey, i added those js files, dont worry”. This is whole
purpose of this question

I think is perfectly fair, I +1ed your Q but the problem is that way does not exists, or better does not exists a clean, canonical way.

How To

  1. first of all you have to look at all the dependencies of the 7 scripts and take note of that for future usage
  2. create a single combined js file for all the 7 scripts
  3. hook wp_print_scripts with a late priority, probably PHP_INT_MAX, and inside hooked callback, check if BuddyPress is installed and activated and if so:
    1. dequeue the 7 scripts
    2. enqueue your single script, using as dependencies for it all the dependencies of the 7 files (the ones you discovered at point #1)

Doing so WordPress will add your single script (that contains the 7 scripts) and all the dependencies needed and everything works.

If a third party BuddyPress plugin will add a script using one of the BP scripts as dependency, e.g.

add_action( 'wp_enqueue_scripts', function() {

  wp_enqueue_script( 'thirdparty-bp-script', $js_url, array( 'bp-jquery-query' ) );

} );

everything will work, because this script run on 'wp_enqueue_scripts', and at that time BP script used as dependency is enqueued, so WordPress will not add it again.

Problems

What can be wrong with this workflow? I think nothing with the workflow itself, but if you want to use this script for a theme to be shared or to be sold, I really want to discourage you to do that.

The reason is that BP scripts have probably changed in various versions of plugin, while maintaining the script handle. So you would need to go back in BuddyPress history, look at that scripts line by line and add different versions of your combined script for every BP version, and enqueue the right script by checking version of BP installed.

Of course scripts may change in future, or some new scripts may be addeed, so again on every BP release, even minor ones, you need to check the scripts nad their dependencies and add a new version of your script if needed. As you can understand this becomes hard to maintain, and you can bet there will be issues for your users.

IMHO this kind of optimizations, even if required in any WordPress installation where performance is important (all?) should be implemented by site owner, in your case by people that will install your theme, not by theme developer.

As example, what about someone may want to combine all the js enqueued in the site, not only the onces used in your theme, and use a CDN for it?

In conclusion, if you want to do that for your own site, follow my steps, it should work, but if you plan to share/sell your theme, leave things as they are.

If you care, you can add a section in your theme docs where explain how to combine scripts for better performance, and let your users choose if do that or not.

Leave a Comment