Plugin Directory

!This plugin hasn't been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.

Optimize Scripts

Concatenates, minifies and optimizes scripts using Google's Closure Compiler. For other scripts (on CDNs), removes default 'ver' query param.

This plugin is developed at Shepherd Interactive for the benefit of the community.

Concatenates scripts and then minifies and optimizes them using Google's Closure Compiler (but not if define('SCRIPT_DEBUG', true) or define('CONCATENATE_SCRIPTS', false)). For non-concatenable scripts, removes default WordPress 'ver' query param so that Web-wide cacheability isn't broken for scripts hosted on ajax.googleapis.com, for example. The following performance rules are supported or enforced by this plugin:

  1. Minimize HTTP Requests, as multiple scripts are concatenated together into one.
  2. Reduce DNS Lookups, since scripts from multiple domains can be concatenated together.
  3. Add an Expires or a Cache-Control Header, which is a prerequsite for this plugin to function and can be implemented with server configuration or optimizescripts_expires filter.
  4. Use a Content Delivery Network: enqueued scripts without a version provided will not be output with a ver query param so that Web-wide cacheability is not broken for static scripts on CDNs (like Google).

This plugin does the following:

  1. Concatenates enqueued scripts together.
  2. Respects head and footer groups.
  3. Minifies code using Google's Closure Compiler.
  4. Caches the concatenated/minified code and rebuilds it only when one of its source scripts expires or changes. The optimizescripts_expires filter is provided which allows the far-future expires time to be forced.
  5. Filename for concatenated/minified script is the md5 of all the handles concatenated together (thus if an additional handle is provided, a new concatenated script is generated).
  6. Provides a filter to limit which scripts get concatenated (i.e. jQuery on ajax.googleapis.com should be left alone); by default, all scripts on local host are concatenated, and remote scripts are left alone.
  7. Removes the default 'ver' query parameter which WordPress adds to every script src if no version argument is supplied on wp_enqueue/register_script: this is important for Web-wide caching of scripts loaded from ajax.googleapis.com, for example. When registering new scripts, pass the filemtime in as the version so that whenever a file changes, the concatenated script will be regenerated.
  8. Script optimization can be delegated to a scheduled cron job so that the server does not periodically respond slowly to visitors.

For example, consider the following code:

wp_deregister_script('jquery'); //remove locally jQuery in favor of Google's
wp_enqueue_script(
   'jquery',
   'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
   array(),
   null, //since empty, no ver parameter in query string
   true //in_footer
);
wp_enqueue_script(
   'myscript1', 
   get_template_directory_uri().'/myscript1.js', 
   array('jquery'), 
   filemtime(TEMPLATEPATH.'/myscript1.js'),
   true //in_footer
);
…
wp_enqueue_script(
   'myscriptN', 
   get_template_directory_uri().'/myscriptN.js', 
   array('jquery'), 
   filemtime(TEMPLATEPATH.'/myscriptN.js'),
   true //in_footer
);

Without this plugin, the previous code would be generated by WordPress as:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=2.8.6"></script>
<script src="http://example.com/wp-content/themes/foo/script1.js?ver=45678901"></script>
…
<script src="http://example.com/wp-content/themes/foo/scriptN.js?ver=12345678"></script>

But with this plugin enabled, the output would be as follows:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="http://example.com/wp-content/js/9e107d9d372bb6826bd81d3542a419d6.js?ver=7659098223"></script>

Important! You must configure your server to serve static JavaScript files with a far-future Expires header! This is a performance best-practice because it reduces the number of HTTP requests that need to be made with a primed cache. Providing the filemtime as the version for the included scripts is essential to ensure that when a script changes, the old cached version is replaced with the new one. If you do not have control over the Expires header of the scripts you enqueue, you may override the default via the optimizescripts_expires filter.

An admin page is provided to give fine-grained control over how scripts are optimized; see screenshots.

I'd love to get your feedback and contributions to make the code better so that we can boost WordPress frontend performance.

See Trac ticket 3372.

Requires: 2.8 or higher
Compatible up to: 2.8.6
Last Updated: 2009-12-4
Downloads: 1,883

Ratings

3 stars
3.4 out of 5 stars

Support

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

100,1,1
0,1,0
100,1,1
100,1,1
0,1,0
100,1,1