Hello, it’s Doug again, this time with a short post on W3 Total Cache (W3TC). If you are developing sites with WordPress, it’s very likely you have heard of W3TC, or you are using it for your site – if not, W3TC is an awesome plugin to help optimizing site performance, do check it out at WordPress Plugin Directory. Here are a few tips and tricks for developers who build their own theme or who is willing to mess with php code a little to get their way.

1. A charset attribute on a meta element found after the first 512 bytes.

So you installed W3TC, lo and behold W3 Validator gives you a strange error. You probably fire up filezilla and start reading header.php, everything looks fine, as a matter of fact, there is not even a space between <head> and <meta>.  If you consult Uncle Google, the 1st couple results will tell you to go and edit minify.php in the W3TC plugin folder, but plugins are updated more often than theme, so here’s an easier way.

<!– W3TC-include-css –>

<!– W3TC-include-js-head –>

Just add these where you want your minified css or js files to be loaded. I’d recommend right after the charset meta to avoid all other possible errors.

2. I turn on JS Minify and W3TC crashed my slider!

Sometimes you will notice the awesome animations on your page are gone after turning on JS Minify. Don’t fret, most likely, the animation js is loaded before jquery due to minified js not loaded first. The simplest way is to go to Minify setting and add jquery js to your exclude list. Voila, problem solved, if not, drop me a comment and we’ll work it out =]

3. W3TC security vulnerabilities!!!

Ok, settle down, it’s an old issue by now, I don’t know if it’s fixed, but there are solutions – in any event, the vulnerability only affected a small group of user with db cache and directory listing turned on if I recall correctly. Easiest way is to open wp-config.php and add the line bellow right after define(‘WP_CACHE’, true); // Added by W3 Total Cache

define(‘W3TC_DYNAMIC_SECURITY’, ‘MySecret’); //replace MySecret with your own string of course.

4. fragment caching, mfunc, & body_class()

So W3TC comes with a cool concept called fragment caching, in which your page cache will cache parts of of your page then when it encounter an mfunc tag, skip it and continue to cache the rest of the page. So at next render, parts of the page are cached and dynamic content can be introduced. E.g. many themes used to use body_class() to apply different classes for mobile and desktop, when page cache is turned on without mfunc, whichever device access the page 1st, the page will be cached with the class for that device – the solution looked to be fragment caching. But mfunc was and seemingly is still buggy, I would recommend avoid it altogether if you can; if you must and probably having it not working, open up PgCache.php search for $buffer and play with the regex in preg_replace_callback.

That’s it, folks. As always, like us on Facebook, follow us on Twitter, join our circle on Google+, and have Happy New Year from all of us at Sysnovo =]