Optimizing script loading, implementation

The “first run” of the script loading optimization is in trunk. It uses both methods: splits the scripts queue in head and footer parts, then concatenates and compresses them before sending them to the browser. Most CSS is also concatenated and compressed.

There are two new constants that disable concatenating and compression: CONCATENATE_SCRIPTS and COMPRESS_SCRIPTS. Setting the first to false would disable¬ both concatenating and compression, the second disables compression only. There is also a simple AJAX method to test if compressing from PHP works as expected on the server. It is run only once and the result is saved as an option. It will¬ run again if that option is deleted.

For plugin authors there are two new actions in the admin footer: do_action('admin_print_footer_scripts'); and do_action("admin_footer-$hook_suffix");. The order of execution is:

  1. "admin_footer" can be used to print scripts before the default footer scripts
  2. "admin_print_footer_scripts" used to print the default scripts followed by any external scripts that were queued for the footer
  3. "admin_footer-$hook_suffix" can be used to print scripts that should appear on a specific admin page only

The preferable way for plugins to add scripts to admin pages would be either to enqueue them properly (which is a must if the script depends on a default script) or to use the $hook_suffix hooks to add them only where needed.

New hook(s) may also be needed in the functions dealing with splitting or concatenating the script queue. Suggestions are welcome either here as comments or on trac as enhancements tickets.

Update: added do_action('admin_enqueue_scripts', $hook_suffix); allowing plugins to easily queue scripts on the exact pages. To find out the value for $hook_suffix on a specific page, echo it from your function and visit the page.

Queueing a script for the footer follows the same syntax as in script loader:

$scripts->add( 'handle', 'full/url/to/file.js', array( 'dependency' ), 'version' );
$scripts->add_data( 'handle', 'group', 1 );

Group 1 means queue for the footer, group 0 (zero) is the default.