A Tool for Configuring W3TC Minify

Although I am still trying to find workarounds my current thinking is that the design of W3TC 0.9.7.5 “manual minify” mode prevents W3TC 0.9.7.5 JavaScript minification in “manual minify” mode from being successful except under some quite restrictive conditions which will not be true for some modern WordPress web pages. You can read my analysis of W3TC 0.9.7.5 JavaScript minification and why I think W3TC 0.9.7.5 JavaScript minification is sub-optimal at my web page for this plugin.

The W3 Total Cache auto minify mode does not work on my web site. The problem is the order of the JavaScript files using auto minify mode is different from the order without minification. This results in undefined JavaScript function errors. W3 Total Cache has a help tool for manually setting the order of JavaScript files. I think this tool is tiresome to use. I just want to have the same order as without minification.

I have written a simple plugin that records the order of the JavaScript files as they are sent. After the last JavaScript file is sent the plugin generates a compatible W3 Total Cache JSON configuration file by modifying the value of the field “minify.js.groups” of the current configuration. This JSON configuration file can then be downloaded (The download link is on the “Installed Plugins” page after the “Deactivate” link.) and optionally further edited to fine tune the minify process. Then this new configuration file can be imported into W3 Total Cache using the “General Settings” page of W3 Total Cache. But, before you import this new configuration file into W3 Total Cache, you should backup the current working configuration by downloading the current configuration of W3 Total Cache using the “General Settings” page of W3 Total Cache. You may want to diff the new configuration file against the current configuration file of W3 Total Cache to verify that the new configuration file has no obvious errors. If the new configuration does not work import the previously downloaded configuration into W3 Total Cache to restore the previous configuration.

Since the ordered list of JavaScript files depends on the template used by a web page, in order to build a complete configuration file it is necessary that for each template a view of a representative web page using that template be done. Each view of a page updates the ordered list of JavaScript files for the template that page uses. This process is cumulative so viewing a representative page for each template eventually builds a complete configuration for all templates.

NB: W3 Total Cache assumes that the ordered list of JavaScript files is constant for a template. If your web pages are dynamically computing the ordered list of JavaScript files and two different web pages using the same template compute different lists of JavaScript files then W3 Total Cache cannot be used to minify those JavaScript files. If the difference is small a possible workaround is to omit the different JavaScript files from the ordered list of JavaScript files using the file “files-to-omit.ini”. This may not work as the order of the files cannot be completely preserved.

The WordPress “Admin Bar” is a problem. This bar requires a JavaScript file – “admin-bar.js”. So, this JavaScript file will be added to or removed from the ordered list of JavaScript files for a template depending on whether you are logged in or not. Unfortunately, every time the log in status changes a rebuild of the W3TC configuration file will be triggered and admin notice will be displayed informing you incorrectly that you have a new W3TC configuration file. To fix this I have added the option to omit certain JavaScript files from the ordered list of JavaScript Files.

For my test website running the Twenty Sixteen theme sample_w3tc_conf_files.zip is a .zip file containing the default W3TC configuration file and the W3TC configuration file generated by this plugin. You can diff these files to understand how this plugin works.

This plugin is still in early development. It has not been extensively tested. It is being made available as is so I can get feedback. Although this plugin works on my website, at this stage of development there are probably many serious problems and only developers who really understand the minify process should try it.

There is a significant restriction when using manual minify mode. W3TC in manual minify mode cannot handle templates loaded using the ‘template_include’ filter. I have a topic in the WordPress support forum on this problem.