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
example. The following performance rules are supported or enforced by this plugin:
verquery param so that Web-wide cacheability is not broken for static scripts on CDNs (like Google).
This plugin does the following:
optimizescripts_expiresfilter is provided which allows the far-future expires time to be forced.
ajax.googleapis.comshould be left alone); by default, all scripts on local host are concatenated, and remote scripts are left alone.
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.
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>
This is a performance best-practice
because it reduces the number of HTTP requests that need to be made with a primed cache.
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
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.