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("!<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:


Posted in: WordPress
TAGS:, , , , , , , , , , , , , , ,
leave a comment.

25 Comments

  1. Beta software.re.or.id
    2 weeks ago

    i want to use PHP speedy thank for the guide.

  2. Viet tanvietblog.tk
    Jul 21st, 2010

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

  3. Pallab pallab.net
    May 1st, 2010

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

  4. Gary Barrett godotdesign.com
    Mar 2nd, 2010

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

    • Vladimir prelovac.com
      Mar 2nd, 2010

      Not in 2008, it didn't ;)

    • Jace
      Mar 19th, 2010

      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.

  5. Sebastian knowtebook.com
    Feb 20th, 2010

    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

  6. jeremy echealthinsurance.com
    Jan 19th, 2010

    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?

  7. Joe Pea
    Jun 18th, 2009

    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!

  8. Eduardo kynes.frenopatico.net
    Jun 13th, 2009

    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.

  9. Trafico web puydi.net
    Mar 24th, 2009

    wordpress load suck :(

  10. Vladimir prelovac.com
    Mar 19th, 2009

    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.

  11. Otto ottodestruct.com
    Mar 18th, 2009

    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.

    • HipSoul Clothing hipsoul.com
      Apr 30th, 2010

      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!

  12. Raju techpp.com
    Jan 27th, 2009

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

  13. Ryan pixopoint.com
    Jan 22nd, 2009

    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.

  14. Antonio
    Jan 15th, 2009

    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

  15. Vladimir prelovac.com
    Jan 12th, 2009

    I think WP reformatted my pre-formatted code :)

  16. Susuteh
    Dec 25th, 2008

    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

  17. Susuteh
    Dec 25th, 2008

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

  18. Susuteh
    Dec 25th, 2008

    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.

  19. scribu
    Dec 23rd, 2008

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

  20. Vladimir prelovac.com
    Dec 23rd, 2008

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

  21. scribu
    Dec 23rd, 2008

    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.

  22. Mike murmatrons.armadillo.homeip.net
    Dec 23rd, 2008

    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.

Have your say

Your email is never published nor shared. Required fields are marked *

*
*

Subscribe without commenting

About Vladimir

vladimir prelovac Hi! My name is Vladimir Prelovac. I am a computer engineer by profession and an adventurer by state of mind.

"I would love to change the world, I just don't have the source code yet."

Books by Vladimir

WordPress Plugin Devleopment Book WordPress Plugin Development: Beginner's Guide

Published by Packt Publishing, available online through Amazon. Click the image for more information.

Consulting Services

Professional WordPress Services, including custom developed plugins and themes

Expert on-site WordPress SEO consulting and an 'out-of-the-box thinking' approach to problems