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!

Make your WordPress plugin talk AJAX

This morning at BarCamp Cork I gave a short talk on how to add AJAX functionality to WordPress plugins.

Here are the example scripts I used during the talk. Rename the files to .php and install as you would normal WordPress plugins.

  1. helloworld1.txt – very simple and basic “Hello World” plugin.
  2. helloworld2.txt – script that will display the text “Hello World” using an AJAX request to get the text from the server.
  3. helloworld3.txt – script that displays “Hello World” text, and an “update” link that increments a counter via an AJAX http request.
  4. helloworld4.txt – script that displays a simple form. The form has one text box, a “Random” button and a “Save” button. Clicking the “Random” button makes a request to the server to get a random number. “Save” sends the number to the server.
  5. helloworld5.txt and rate.txt – “Rating” script to rate a post (Originally from here). Makes an AJAX request to the server with the rating. Server returns randomly generated stats on rating. Place rate.php in wp-content/

All scripts that operate in the Settings page in the Dashboard use a nonce for security and access admin-ajax.php when making AJAX requests. The wp_ajax_$POST[ ‘action’ ] hook is used to execute the actual code that does something useful.

The rating script uses wp_enqueue_script() to load the jQuery class.

If you’re an Irish O2 user, and use Twitter you might like to install the new Tweet Tweet WordPress plugin. Thanks to Enda who let me use his O2 account briefly, I created a plugin that sends Twitter sms notifications using the free O2.ie web texts. That means Meteor, Vodafone and O2 are now covered. Anyone want to contribute a plugin for 3?

PS. I was very proud that Automattic was able to sponsor BarCamp Cork, and hopefully most of the WordPress badges found good homes! Conor says 116 signed up for the event, with just over 100 showing up. That’s a great number to pull in for a Saturday tech event!

IMG_4625

IMG_4724

More photos can be found on pix.ie and flickr. Looks like Phil hasn’t uploaded any of his shots yet! Can’t wait to see what came of his afternoon photo session. I had to leave early and missed it unfortunately.
Discovered photos of my presentation. Yes, I used Vi (or Vim if you want to be pedantic) to give my talk!