WPSCMin: HTML Minify plugin for WordPress and WP Super Cache
WPSCMin is a plugin to a plugin: A plugin for the static-caching WordPress plugin WP Super Cache, it uses Minify to strip whitespace and comments from your web pages. Your super-fast static-cached pages will shrink a further 5-20% in size.
If you used a previous version of my code, the good news is that it’s now a fully fledged plugin to WP Super Cache, so it’s an easy install (no more patches! no more breakage when Super Cache changes!).
What it does
- Minifies HTML pages that WP Super Cache saves to its static caches (including inline JavaScript and CSS style declarations). In my tests, uncompressed HTML pages shrink a further 10-20%, while gzipped pages shrink a further 3-10%.
- Adds a handy “on/off” control to the Super Cache options screen in the WordPress backend. So, you can turn it off for debugging, or to read your pretty indented source code.
What it doesn’t do
- Minify standalone JavaScript and CSS files. I prefer to do these as part of a build process, so they’re both small and static. (I’m a nerd about this: My build scripts even use advdef/7-Zip/zopfli to squeeze an extra 10% over
gzip -9
.) - Minify dynamic pages. The CPU cost of minifying a dynamic page greatly outweighs the bandwidth savings — minifying HTML only makes sense in concert with a static-caching solution like Super Cache, or if you’re using a caching front-end proxy like Varnish or Squid. The whole point of my plugin is that it doesn’t waste resources minifying dynamic pages, only those cached by Super Cache.
If that didn’t make sense, or for more discussion, feel free to take a look at my original release post, where I get into greater detail.
Installation and upgrade
Last update: Updated to version 0.7 on 9/28/2015. Tested with Minify 2.2.0.
Requirements: PHP5. WP Super Cache 0.9.9.6+. (See advanced usage below to use with older versions of Super Cache.)
- Download the current version and unzip. Move
WPSCMin.php
into theplugins
subdirectory of WP Super Cache. If yours is a standard WordPress install, this will bewp-content/plugins/wp-super-cache/plugins/
. - Download the current version of Minify and unzip. Move the
min
directory into theplugins
subdirectory of WP Super Cache, right next toWPSCMin.php
. - Log into your site, go to the WP Super Cache config page, and scroll to the bottom. Enable HTML Minify, and you’re done!
Note to upgraders: If you’re upgrading from version 0.4, you only need to replace WPSCMin.php
with the new version of the file (and that’s it!). If you’re upgrading from version 0.3 or older (the ones with all the patches), be sure to replace your patched Super Cache files with fresh ones, and notice in the instructions above that the location of WPSCMin.php
and min
changed.
Advanced usage
Version 0.5 added several optional features for advanced users. Read on to learn how to:
Mark sections of HTML so that they will not be minified
If there are sections of your pages that you don’t want to minify (for instance, certain HTML comments like AdSense slot IDs), you can tell WPSCMin to skip them. Just surround whatever HTML code you don’t want minified with <!--[minify_skip]-->
and <!--[/minify_skip]-->
. (By the way, these “minify_skip” tags are themselves stripped when the page is minified.)
Install Minify in an alternate location or point to an existing install of Minify
Generally, having more than one copy of the Minify PHP files on your system shouldn’t cause a problem, because WPSCMin checks to see whether Minify is loaded before including it again. However, if you want to point to a Minify min
directory somewhere else on your filesystem, you can now do that.
Edit your WP Super Cache config file wp-cache-config.php
and create a new line that sets the variable $cache_minify_path
to the full filesystem path of the directory containing the Minify min
directory. It should look something like this:
// i.e. if Minify were in /some/path/to/a/dir/min
$cache_minify_path = '/some/path/to/a/dir';
Use with an older (0.9.9.5 or earlier) version of WP Super Cache
To use with Super Cache versions 0.9.9.3-0.9.9.5, there’s some compatibility code you can enable by editing WPSCMin.php
. There are three commented-out lines that you will need to enable and then two lines immediately following that you must delete or comment out. These code sections are well-marked and should be easy to spot if you open WPSCMin.php
in your favorite editor and search for the text “For versions of WP Super Cache 0.9.9.5 and earlier”.
For even older versions (and this is really unsupported) of Super Cache, the above compatibility fix may work fine (it’s just untested prior to 0.9.9.3). If not, well, you should really upgrade WP Super Cache or just not worry about minifying your HTML! But, if you still aren’t deterred, take a look at the changelog and see which of the 2008-2009 releases (which require patching Super Cache) may be compatible with your version of Super Cache. Then go to the old files and installation instructions.
Comments, bug reports
Please add them here!
Donncha
Nov. 16, 2010, 8:30 am
Hey there!
I’d like to distribute this plugin inside WP Super Cache. I’ll be releasing a new version tomorrow, or the day after so it’d be a good opportunity to get it out there. What do you think?
sticks
Nov. 16, 2010, 12:23 pm
It’s fine with me, sounds great! I haven’t released this code under a public license, so if you want to slap GPL2 on it (since that’s what you and WordPress are using) that’ll work for me.
Only 2 other thoughts I have are that you’ll also need to package Minify with it … Steve Clay might be a good person to check in with about that (if you haven’t already). Lastly, since this all requires PHP5, you might want to wrap the code in something that does a PHP version check (at least until WordPress drops PHP4 support, which will be a happy day).
moreivyou
Nov. 25, 2010, 9:12 am
@DONNCHA is a good idea “I’d like to distribute this plugin inside WP Super Cache….”
sticks
Nov. 25, 2010, 11:09 am
Donncha and I emailed last week and it looks like this will probably still happen, but not in the 0.9.9.7 release from a day or two ago. So, look for it soon, just not quite yet.
Lazy
Nov. 23, 2010, 10:18 pm
really great plugin, works fine. thank you very much :)
by the way.. it should not strip the adsense comments (start / end..) – would be better to let them in the templates (?)
Have a nice day
Chris
sticks
Nov. 23, 2010, 11:41 pm
You’re welcome! As far as AdSense goes, there shouldn’t be any technical problem caused by minifying the HTML code snippet (i.e. removing the JS comment). Google has also said that it’s OK to remove the channel comment (i.e. it’s not a TOS violation that’ll get you banned from AdSense or anything like that … remember no AdSense code is modified, just HTML and JS comments).
If you want to minify your pages but keep the AdSense comment intact, here’s a quick hack to do that (and some explanation on why there’s no better way to do it … yet). Or, if you’d like to be able to mark a code block to not be minified (i.e. with “<!– minify-skip –> <!– /minify-skip –>” or something), you could suggest that to the Minify project.
Lazy
Nov. 24, 2010, 3:16 am
thank you very much for your good, nice and fast answer! :)
have a really good day! ;-)
sticks
Jan. 19, 2011, 10:07 pm
FYI, version 0.5 that I’ve just released adds the ability to mark sections of code so that they won’t be minified. See above for instructions.
beginner
Nov. 24, 2010, 4:38 pm
hi,
I’ve just found this WPSCMin and then install WPSCMin-0.4 and Minify 2.1.3. After enabling HTML Minify, all of my single post is turning to white pages without any page sources shown. I’ve following all of your instructions above and my WP Super Cache versions is the newest : 0.9.9.7. Did I do something wrong?
sticks
Nov. 25, 2010, 11:20 am
I’ve upgraded to WP Super Cache 0.9.9.7 without any problems, so, short answer is that there aren’t any known issues. And, can’t imagine how Minify would affect single post pages differently from other pages (it’s just a one-line function call on the Super Cache buffer contents), so … yeah … it’s probably something specific to your configuration (maybe a bug in Minify is interacting strangely with something in your template?). If everything was not working, I’d say to check your PHP5 version.
I don’t want to turn this comment thread into a support forum, but I’ll email the address you submitted and see if I can help, and report back anything useful here.
Rhiannon
March 22, 2011, 7:51 am
I had this same problem, except for me it was on all pages.
Turns out the cause was the use of require / include in my theme’s functions.php.
Can you confirm this, and provide a fix? Thank you :)
sticks
March 22, 2011, 11:04 am
Thanks for the report. This one was a mystery to me and when people leave reports and then disappear it isn’t very helpful. :)
Can you be more specific? (What theme, what exact lines of code does it have?)
WPSCMin already tries not to do a double-include. It checks to see if the Minify_HTML class exists before including the library, and then it does a require_once to prevent a reinclude. Please see install minify in an alternate location above for more info.
If your theme is doing a clumsy include *after* WPSCMin, you could just add a class-check to your theme, and make sure it is using require_once/include_once. i.e. instead of “require(‘x’);” do “if (!class_exists(‘Minify_HTML’)) require_once(‘x’);”.
Also, a note to Beginner … anytime your site is just spitting out blank pages it’s likely there’s an error message but you’re choosing not to show it. You can change the PHP error level at runtime to show errors on-screen (you can use WP_DEBUG to do this, or just do error_reporting(E_ALL);), or you can log errors to a file so visitors never see them but you know where to find them (see php.ini).
moreivyou
Nov. 25, 2010, 9:08 am
Brilliant!
Sahar
Nov. 29, 2010, 10:11 pm
Great SuperCache plugin! Thanks.
I Think it can be great to let the user/developer define custom path to minify. (For example, I already use minify, placed it on root, and don’t think it’s a good idea to have duplicate folder on minify within one project.
sticks
Nov. 29, 2010, 11:09 pm
That’s a good idea — I’ll improve how this works for the next release.
Right now, WPSCMin checks to see whether the PHP class
Minify_HTML
exists already or not, so if you’ve already included/required your own copy of Minify, it doesn’t try to re-do it. Or, if you put the “min” directory of Minify somewhere in your PHPinclude_path
, WPSCMin’srequire_once
statements will find it no matter where it is. So, those are two ways using the current version that you don’t actually have to put Minify in the plugins subdirectory of WP Super Cache (despite what the instructions say above).Alternately, check out WPSCMin.php at line 93 if you want to just hardcode a different file path into the current version of WPSCMin.
Sahar
Nov. 30, 2010, 12:40 pm
Thanks, I already inserted hardcode path to the current folder i’m using.
Looking forward to the next version.
Btw, what’s up with the plan making WSPCMin part of Wp-Super-Cache package ?
sticks
Dec. 1, 2010, 1:57 pm
You’d have to ask Donncha, but it sounded like he just already had enough new stuff in his 0.9.9.7 release.
One issue I can foresee with mass distribution is there are people still running PHP4 (6+ years after PHP5), but Minify is PHP5. So, one thing I’ve put in for the next release is just to wrap all the code in an if statement that checks the PHP version.
(btw, I’ve committed some code that lets you specify an alternate path to Minify in your wp-cache-config.php file, so that’s ready for my next release.)
Vamban Blog
Dec. 1, 2010, 6:27 am
Simply superb.
Ping from Photoblog performance tips: Day 7 – Caching on Phill Price | Photography – A daily photograph from London, England
Jan. 1, 2011, 6:18 am
[…] earlier in the week to the JavaScript and CSS. There’s a perfect guide to setting it up at Lyncd, so I won’t duplicate it.Now log out of the system and set YSlow and Page Speed running, and for an external benchmark, set […]
Ray
Jan. 2, 2011, 2:03 pm
Checking out your plugin.
Works good, but if you have “Don’t cache pages for known users.” enabled, the plugin still minifies the page. I cleared the cache in the WPSC admin to double check.
Tested on WPSC 0.9.9.7 and latest version of WPSCMin.
sticks
Jan. 19, 2011, 6:38 pm
Thanks for the report, I’ve replicated this on WPSC 0.9.9.7-8.
I’ve got a new public release almost ready to go, and will disable minify when Super Cache detects a known user and that option is enabled. Look for it in the v 0.5 release in a day or two.
Ping from The Quest For Speed | W-Shadow.com
Jan. 18, 2011, 5:41 am
[…] strip redundant whitespace from my pages and re-organize tags for better compressibility.ResourcesWPSCMinWP Super CacheWP MinifyUse CloudFront CDN with origin pullOn the one hand, using a Content Delivery […]
Christopher Fisher
Jan. 22, 2011, 8:19 am
I recently installed WPSCMin with the latest version of SuperCache. As soon as I turned on WPSCMin, my entire website went down with an internal server error. This is obviously difficult to test since it brings down a live site, and I cannot have my website down while figure this out, but I will see if I can test on a development version of my website.
Do you have any reports of this error? I’ll report back with what I find out.
Thanks!
Chris
sticks
Jan. 22, 2011, 1:49 pm
Thanks for the report. No, you’re the first. Are you using the new 0.5 release of WPSCMin or 0.4? (I’d be especially interested if it’s the 0.5 version which is only a few days old.)
Feel free to e-mail me at the address on the about page and I may be able to help debug. On the live server you should at least double-check your PHP version and the filesystem locations of both WPSCMin.php and the min directory. Definitely a good idea to test on a dev version first!
Christopher Fisher
Jan. 22, 2011, 3:22 pm
It is indeed .4. I reinstall .5 and try to report back by tomorrow. I’m sure it is something I’m doing wrong, but I’ll contact you through your about page if things head south. Thanks for the fast reply.
Sandro Franchi
May 25, 2011, 7:53 am
It’s just perfect, I’m using both wp-supercache and your plugin and the results are just perfect.
Thanks a lot!
Ping from Blog-Coaching Woche 2 – Technik-Check »
June 19, 2011, 9:54 am
[…] Addon. Dazu liefert WP Super Cache gute Tipps in einer Hinweis-Box. Daher habe ich zusätzlich WPSCMIN installiert. Eine abgespeckte Variante von WP Minify, welches nochmal HTML Dateien shrinkt. Zuerst […]
andre
July 18, 2011, 11:04 pm
Hmmm… I’ve been trying to install a cache system on a newly built site and I’ve had poor results with Supercache compared to W3TC (nothing like CDN or memcache, etc) with its own Minify. So I was hopeful when I found your plugin integrated to Supercache… but still I get lower speed and Yslow results than with W3TC.
Reason I’m still looking is despite higher results, the pages don’t serve consistently faster. Some do, others lag for about 15/20 seconds, the admin section also is unevenly served.
I’m using Genesis framework and Platinum child-theme, on BlueHost, with Wishlist membership plugin. I’m desperately looking for a solution that will wow my client!
Many thanks!
Regards,
Andre
sticks
Aug. 12, 2011, 12:38 pm
Yeah, Minify isn’t going to make any sort of difference of that magnitude, and it’s only going to affect cached pages — minifying dynamically slows things down. And Minify is for when you want to shave 5-10 ms off a page that Apache is taking 100 ms to serve, not when you’ve got Apache/PHP overloaded and they’re taking seconds to serve pages.
If things are that slow, there’s almost certainly some bad code — bad PHP, slow MySQL queries — you really need to audit the code. If your host’s Apache is taking that long to serve static files, you really need to upgrade the hosting. Good luck!
aandroid
Aug. 2, 2011, 6:40 pm
Its help to manage my blog. i hope at future my blog more and more fast. That google love to visit. thanks for the review.
Ping from Tekedia » Higher Performance Wordpress Blog – Recommended Links And Plugins
Aug. 9, 2011, 9:02 am
[…] served by your web server by joining Javascript and CSS files together. Alternatively you can use WPSCMin, a Supercache plugin that minifies cached pages. It does not however join JS/CSS files […]
Ping from Guide till en säkrare och snabbare WordPress-sida | FS Data Blogg
Sept. 22, 2011, 6:14 am
[…] Till sist så aktiverar vi ”HTML Minify” under fliken Tillägg. Detta kräver en liten installation, som du hittar detaljerade instruktioner till här: WPSCMin Installation. […]
Goutham DR
Nov. 6, 2011, 5:09 am
This plugin really works…:)
xiangzhuyuan
Jan. 4, 2012, 4:41 am
nice plugin, thanks!
Ping from - forexgrad | forexgrad
Jan. 24, 2012, 7:54 pm
[…] served by your web server by joining Javascript and CSS files together. Alternatively you can use WPSCMin, a Supercache plugin that minifies cached pages. It does not however join JS/CSS files […]
Ping from Wordpress Plugin Empfehlungen | Stefans Blog
Feb. 25, 2012, 6:16 am
[…] es die Möglichkeit Plugins für das Super Cache Plugin zu installieren. Empfehlenswert ist hier HTML – Minify. Dadurch minimiert Super Cache zusätzlich die ausgelieferten HTML – […]