HOWTO: Make WordPress plugins work with WP Super Cache

Daffodil, snow and water droplets Thaya Kareeson has written an excellent article for plugin developers. It goes through how to make plugins work with WP Super Cache by using dynamic AJAX calls.

WP Super Cache can make static html copies of pages served by WordPress which is great for performance. Unfortunately that means some plugins don’t work because they rely on executing PHP on each request. The plugins need to be rewritten to use AJAX calls by the visitor’s browser. There’s a FAQ in the readme.txt all about it!

I previously wrote about adding AJAX to WordPress plugins but Thaya has worked through a simple example that will work perfectly with WP Super Cache. It’s a good foundation for plugin developers start from.

He also has versions of WP Postviews and Popularity Contest that have been rewritten to support static caching. I haven’t tried either plugin so leave a comment on his blog if you need help!

If you depend on a large portion of your content being dynamic this isn’t the solution for you as it will affect what search engines see. Those bots don’t speak Javascript, but for interactive purposes (ratings, stats etc) it’s the job.

reading glasses Further reading:

  • AJAX in Plugins is a must-read starting point for developers.
  • wp_enqueue_script() is the command WordPress uses to load Javascript files. That page links to a couple of good pages too including this best practices post. As of this writing, Thaya’s example don’t use wp_enqueue_script() but it’s simple to use.

The vast majority of plugins work just fine with WP Super Cache, but some of the ones that don’t are quite popular. If your favourite plugin doesn’t work, why don’t you help the author out and fix it? You have all the source code after all and you’ll be helping everyone else who uses the plugin!


You might also like

If you like this post then please subscribe to my full RSS feed. You can also click here to subscribe by email. There are also my fabulous photos to explore too!

Published by

Donncha

Donncha Ó Caoimh is a software developer at Automattic and WordPress plugin developer. He posts photos at In Photos and can also be found on Google+ and Twitter.

23 thoughts on “HOWTO: Make WordPress plugins work with WP Super Cache”


  1. I actually just had to do this exact thing for a contract gig. The client was using WP Super Cache and could not figure out why WP-Postviews wasn’t updating on refresh. I fixed it with AJAX, just like the author above.


  2. Thanks for writing about my post Donncha.

    I wrote it because I still cannot use WP Super Cache on my site because of Who Sees Ads and a few other plugins. I would make them compatible myself, but there is definitely no time to do it all myself.

    I was hoping that I can help urge all plugin developers make their plugins WP Super Cache compatible and I can eventually reap the rewards.


    1. It certainly could but in most cases the number of anon users vastly out numbers the number of logged in ones so there wouldn’t be much of a performance gain.

      In recent versions of the caching plugin the number of cache files has been reduced dramatically too.


  3. Since WP Super Cache is very popular plugin and very useful, I am working on improving my GD Star Rating plugin to support it. Rating already works (partially at least, votes are saved), but is not always displayed right. This post is good starting point to add cache support.


  4. Just wondering what the other plugins are that this plugin doesn’t work with. I have installed super cache but on a recommendation from someone. I haven’t actually done my due diligence on how to make sure it works properly. I just was concerned about my site loading faster.

    Thanks,
    Lisa


  5. Lisa – plugins that update the page based on user interaction may not work. I’d say most plugins will work just fine.

    Android – check the supercache page, there’s a big donate link there and on the admin page too!


  6. Sorry im not very newbie in php but what is ajax? from where can i learn something about this programmin language? and can you advise me some books name related to ajax? thanx


  7. Excellent! That’s exactly what we need. As someone new to blogging, I’m afraid to use a caching plugin for that very reason. I don’t know how to tell what it will break.


  8. I use the “More Fields” plugin, which lets me add a “File List” Custom Field to my Add/Edit Post pages. The “File List” control works by issuing an AJAX request to “/wp-content/plugins/more-fields/more-fields-write-js.php?post_id=xxx&…”, but that request fails when WP Super Cache is enabled — even when I explicitly add “more-fields-write-js\.php” to list of strings not to be cached.

    Here’s why: at the time the AJAX request is made, I have to be logged in, and the effect of being logged in causes WP Super Cache to append the string ‘‘ to the AJAX result, which effectively mangles the JSON string being returned.

    My solution was to rearrange a couple of tests being performed in wp-cache-phase2.php, so that it *FIRST* checks “if (!in_array($script, $cache_acceptable_files) && wp_cache_is_rejected($wp_cache_request_uri))” *BEFORE* it checks “if ( $wp_cache_not_logged_in && is_user_logged_in() && !is_feed() && !is_admin() )”.

    This simple change insures that anything I explicitly block from being cached won’t be modified either. This allows “More Fields” to work properly, and it allows me to easily fix any other incompatible plugins that I might run into.

    If this change is safe enough to incorporate into WP Super Cache, that would be ideal — or if you have a better solution, even better. In any case, thanks for a great plugin!

Leave a Reply