This advanced article is a part of WordPress Optimization effort.
PHP Speedy allows us to automatically combine all JS and CSS files into only two files - which in turn helps greatly with page loading times. Unfortunately, PHP speedy comes with few flaws out of box that need fixing:
- Combined JavaScript is placed at the top of the page instead at the bottom
- It does not work with WP Super Cache
In order to get this two working like we want them, we need to make few adjustments to both plugins.
First PHP Speedy (version 0.5.1). Open file libs/php_speedy/controller/compressor.php and around line 441 replace:
$source = preg_replace("!<head([^>]+)?>!is","$0 \n".$newfile."\n",$source);
with
if ($options['save_name']=='cssstylesheet') $source = preg_replace("!<head([^>]+)?>!is","$0 \n".$newfile."\n",$source); else $source = str_replace('</body>', $newfile.'</body>', $source) ;
That will move the JavaScript to the bottom of the page. Then we need to modify WP Super Cache (0.8.6). At around line 461 in wp-cache-phase2.php add this code so it becomes:
flush(); //Ensure we send data to the client // php speedy patch global $cache_filename; $mtime = @filemtime($cache_path . $cache_filename); if( !((!$file_expired && $mtime) || ($mtime && $file_expired && (time() - $mtime) <5)) ) { $new_cache = true; } if ($new_cache) {
This will get the two working along. I suggest turning all PHP speedy gzip, minify and data_uris options off, and leave the compression to your server (through .htaccess).
One more improvement I can see in PHP speedy, is grabbing all JavaScript code from the page and combining it too (by this I mean code referenced directly in the page and not in external files).
With the help of this technique among others I am able to have only 6 HTTP request on my main page and a YSlow score of 94/100 as far as my WordPress installation goes.
See also:
- How to Speed up WP Super Cache
- Optimize your WordPress page loading times with a simple tweak
- WordPress Plugins
Posted in: WordPress
TAGS:can run, combine wordpress, javascript load php, optimize javascript load, optimize wordpress loading times, php speedy, php speedy supercache, php time pages, super comment problem, super website doesn update, supercache wordpress, supercache wordpress main page, wordpress long page problem, wordpress page load timer, wordpress page loading time, wordpress php page compressor
Hi! My name is 
25 Comments
i want to use PHP speedy thank for the guide.
It didn't work W3 Total Cache well. Can you give me some instructions to solve this problem ? Thank you for your post
Can you update this guide for the new version of Wp-Super cache?
Doesn't W3 Total Cache essentially replicate what both PHP Speedy and WP Super-cache do in a single plugin?
Not in 2008, it didn't ;)
Gary, I don't think so. One of main differences is that W3 Total Cache doesn't seem to combine JS & CSS files. Minify yes, combine the files to reduce the number of http requests... no.
Vladimir, does the latest version of Php Speedy (0.5.2) still conflict conflict with WP Super Cache out of the box? If so, do your instructions above still accurately describe the fix? Thank you.
Hey! We just have a discussion aboout that on http://www.knowtebook.com/11-hot-tips-and-tricks-to-speed-up-wordpress-1705.htm#comment-5658 if it works with WP Super Cache or not. Please join and let 50.000 readers know your thoughts.
Yours, Sebastian
I did the PHP Speedy thing and it is amazing by itself, my site is so fast on my self hosted server that it almost seems unnatural. I am not using WP Super Cache, and now can't see the point in it anymore as my site is blazing fast. I am using though hyper cache and aa apache crazy cache which work right out of the box. Any thoughts on this configuration?
Hey Vladimir, I have PHP Speedy 0.5.2 and the code is slightly different.
The similar code I found is:
if(is_array($scripts)) {
$script_out = implode("\n",$scripts);
$script_out = "" . $script_out . "";
$content = preg_replace("!]+)?>!is","$0 ".$script_out,$content);
Can you help me figure out what to replace so the Javascript appears at the bottom? Thanks!
Hi Vladimir,
Can you update your solution for the latest WP Super Cache release?
I've tried to patch wp-cache-phase2.php by myself, but with no luck.
wordpress load suck :(
Premature optimization is done at the WordPress core level so there is little we can do about it.
Techniques that involve combining javascripts and CSS files together have been known for long time and have proven to be very effective especially for high traffic sites - where this kind of improvement is most measurable.
Ugh. I didn't know what PHP Speedy was, so I looked it up.
The concept reminds me of the words of the wise man, Don Knuth:
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."
Seriously, if this sort of thing is causing a measurable improvement, then you have some other major bottleneck causing your main problems that you should be addressing first.
Exactly, it's called "Wordpress".
I also notice your site is terribly unoptimized Otto... you have over 23 javascripts loading in the head of your blog (TWENTY THREE!)... and you have 9 external stylesheets as well!!
Each one of those is causing a delay on the loading of you site... to the tune of: 24.5 seconds download time... I'm sorry man, that's worse than a grade "F"... you want to be shooting for 3-5 seconds, with 7 seconds being on the edge of 'miserable'... people just don't have the patience to wait for half a minute, that's just the way things are online.
WORD OF ADVICE FOR EVERYONE:
If all the little social gimmiks you add to your website, end up slowing it down to the point where people LEAVE your site... what's the point?
You can have the coolest website in the world... but it's useless if people don't have the patience to use it... that's where "page caching" and "minify" come in hand.
PHP Speedy is excellent btw, it just needs page caching built-in... then it would be a perfect replacement for W3Total, SuperCache, etc!
Hi,
looks like wp-super-cache has been fixed by the author in v0.9, can you validate this please?
Thanks for the tips :)
I had no idea there was a convenient way to combine javascript files together like that. It will be very useful for those using lots of plugins which each load new javascript files.
Hi,
thanks a lot for the post.
You say to dont activate gzip, etc and to do it with .htaccess
Could you say us or to point us where and how to do it with .htaccess???
thanks again
I think WP reformatted my pre-formatted code :)
Vladimar,
Have some problem with WP-super Cache, after modifying
when refresh the blog, i got this message
Parse error: syntax error, unexpected $end in /home/xman001/public_html/susuteh/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 583
how to fix this?
thanks and Merry X'mas
Vladimir,
Sorry, pls ignore the earlier comments, is working perfectly now.
Thank you.
Hi Vladimir,
I had install and configure PHP-Speedy as per your guild.
Thanks for that, is really fast,,,,
However when I click on my website, the display doesn't show properly.
I using you theme Blue Grace, but it show in list WITHOUT the theme.
But when I click on any article of my, it display as normal.
What going on ?
Kindly assist.
Well, you didn't mention that in your article. :P
These kind of optimizations are performed on expert level and in controlled environment (read: you are supposed to know what you are doing :)
As long as php speedy doesn't grab the inline js, moving the scripts to the botton of the page can cause serious problems: Think of jQuery lib loading after a line of jQuery in the body.
Vladimir,
Can you explain why you're doing what you're doing with the SuperCache modification. I don't get it.
There was a WP support forums topic recently where it was suggested that the incompatibility between Speedy and SuperCache was related to stacked output buffers.
http://wordpress.org/support/topic/187936
I'd like to understand why PHP-Speedy doesn't work with SuperCache so I can modify my version of SuperCache if it's appropriate to do so.