Optimize WordPress page loading time with PHP Speedy and WP Super Cache

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("!]+)?>!is","$0 \n".$newfile."\n",$source);


if ($options['save_name']=='cssstylesheet')
	$source = preg_replace("!]+)?>!is","$0 \n".$newfile."\n",$source);
	$source = str_replace('', $newfile.'', $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.

More like this:

Posted in: WordPress
TAGS:, , , , , , , , , , , , , , ,
Both comments and trackbacks are currently closed.


  1. Feb 26th, 2011 9:26 PM

    Good Tips, Thanks for sharing this this :)

  2. Aug 22nd, 2010 11:43 PM

    i want to use PHP speedy thank for the guide.

  3. Jul 21st, 2010 8:42 AM

    It didn't work W3 Total Cache well. Can you give me some instructions to solve this problem ? Thank you for your post

  4. May 1st, 2010 6:49 PM

    Can you update this guide for the new version of Wp-Super cache?

  5. Mar 2nd, 2010 4:55 PM

    Doesn't W3 Total Cache essentially replicate what both PHP Speedy and WP Super-cache do in a single plugin?

    • Mar 2nd, 2010 5:06 PM

      Not in 2008, it didn't ;)

    • Jace
      Mar 19th, 2010 2:41 AM

      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.

  6. Feb 20th, 2010 1:08 AM

    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

  7. Jan 19th, 2010 7:36 AM

    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?

  8. Joe Pea
    Jun 18th, 2009 2:28 AM

    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!

  9. Jun 13th, 2009 3:09 PM

    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.

  10. Mar 24th, 2009 10:13 PM

    wordpress load suck :(

  11. Mar 19th, 2009 12:29 AM

    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.

  12. Mar 18th, 2009 7:22 PM

    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.

    • Apr 30th, 2010 11:18 PM

      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.

      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!

  13. Jan 27th, 2009 10:17 AM

    looks like wp-super-cache has been fixed by the author in v0.9, can you validate this please?

  14. Jan 22nd, 2009 1:09 AM

    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.

  15. Antonio
    Jan 15th, 2009 9:48 PM


    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

  16. Jan 12th, 2009 11:47 AM

    I think WP reformatted my pre-formatted code :)

  17. Susuteh
    Dec 25th, 2008 11:37 AM

    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

  18. Susuteh
    Dec 25th, 2008 11:25 AM

    Sorry, pls ignore the earlier comments, is working perfectly now.
    Thank you.

  19. Susuteh
    Dec 25th, 2008 11:20 AM

    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.

  20. scribu
    Dec 23rd, 2008 7:16 PM

    Well, you didn't mention that in your article. :P

  21. Dec 23rd, 2008 7:14 PM

    These kind of optimizations are performed on expert level and in controlled environment (read: you are supposed to know what you are doing :)

  22. scribu
    Dec 23rd, 2008 7:12 PM

    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.

  23. Dec 23rd, 2008 6:55 PM


    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.


    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.