[wp-hackers] JavaScript,wp_enqueue_script and concatenation

Eric Martin eric at ericmmartin.com
Sat Aug 1 15:46:34 UTC 2009

I use jQuery on my site and a handful of jquery plugins. I've been
doing quite a bit of thinking about ways to concatenate the scripts
together in order to reduce the number of requests made, while
maintaining the usefulness and features provided by wp_enqueue_script.

For example, I could just concatenate the files and the put it
directly into my template or use wp_enqueue_script to load it, but
then I lose all of the benefits of wp_enqueue_script for each of the
scripts contained in the concatenated version.

At first I was thinking of trying to do it automatically, but I'm not
sure that I want that overhead. You could "cache" the concatenated
script, but I think it could be easier.

Then, I was thinking that perhaps a check could be added to see if the
source and version are the same for scripts added through
wp_enqueue_script and only print out one script tag.

I couldn't find any easy filter or action to use to attempt to do this
myself, so I wanted to throw it out and see if there were any other
ideas out there.

wp_enqueue_script('jquery-fontavailable', get_bloginfo('template_url')
. '/scripts/jquery.fontavailable.js', array('jquery'), '1.1', true);
wp_enqueue_script('jquery-scrollto', get_bloginfo('template_url') .
'/scripts/jquery.scrollto.js', array('jquery'), '1.4.2', true);
wp_enqueue_script('jquery-localscroll', get_bloginfo('template_url') .
'/scripts/jquery.localscroll.js', array('jquery'), '1.2.7', true);
wp_enqueue_script('jquery-simplemodal', get_bloginfo('template_url') .
'/scripts/jquery.simplemodal.js', array('jquery'), '1.3', true);
wp_enqueue_script('easyretweet', get_bloginfo('template_url') .
'/scripts/easyretweet.js', array('jquery'), '1.0', true);
wp_enqueue_script('emm', get_bloginfo('template_url') .
'/scripts/emm.js', array('jquery'), '1.0', true);

Which I'd like to end up printing out 3 scripts tags, one for each
built-in script (jquery & jquery-color) and one more for the scripts
in my theme.

More information about the wp-hackers mailing list