Caching WPtouch themes with WP Total Cache

How to make W3 Turbo Cache play more cleverly alongside the WP Touch iPhone interface plugin.

THE COOKIE NAME HAS CHANGED, PLEASE ADD wptouch_switch_toggle INSTEAD

I really like WordPress. I like the interface and the available plug-ins. It’s impressive how much you can do without coding, just configuring things. I use WPTouch to provide an iPhone/mobile devices theme. However, as a PHP application WordPress famously doesn’t scale to being Fireballed, Slashdotted, Dugg when you’re running on a small server. Even if you’re not too worried about being popular, by the time you’ve installed a few plugins, your site might be feeling a bit sluggish.

Like anyone with the naive belief I could write just such a post, but sensible enough to know it’ll too late to fix if I do, I’ve always had a caching plugin installed. Firstly it was WP Supercache, but now I use WP Total Cache (W3TC).

Short of using a caching front-end server such as Varnish (which is my next side project), WP Supercache/WPTC’s “disk (enhanced)” Page-Caching is the fastest way to serve your pages, because it’s done an Apache level. PHP isn’t loaded, databases aren’t queried, static files are served off disk.

W3TC also does JS/CSS minification, compression, and CDN distribution. These also can accelerate your site, but again won’t always work with all the plug-ins that you have installed. I would suggest you start off with basic caching, enable modules selectively, and test many times.

Caching, and things like themes don’t really co-exist that well, especially if you try and share the same URL for one bit of content rather than having the m.mysite.com thing. The normal install guides for WPtouch and caching plug-ins involve preventing page caching for all mobile user-agents. While it works, it’s not the most efficient use of your server, especially given people clicking on links from Twitter clients on mobile devices may prove part of the onslaught.

Rather than preventing all mobile traffic being cached, what you can do instead is request that pages are cached until “wptouch_toggle_cookie” is set. This is the cookie which allows users to jump back to the desktop view of the site on a mobile device. In this way you’re caching the first hit to your site, and all hits until they change theme. That should cover a lot of visitors.

You have two parallel caching silos: The normal site, and the mobile site. You direct to the correct one of these based on user-agent, and when the user manually flips, the cookie is set, and you’re no longer doing page level caching. You still getting the benefit of the Database and Object caching that WP Total Cache does, but it’s much slower. If you are lucky enough to be suddenly very popular, hopefully the vastly reduced load of the majority of users should mean that even non-cached users get the right content, and not a 500 error.

I’m not going to cover installing W3TC because the plugin itself covers this quite well, and other places. If you’re on Debian I’ll flag up that you can install all the additional libraries like APC/memcached for caching, Curl for CDN uploads etc from packages, and avoid compiling anything.

WARNING: this is potentially brittle. If the cookie name changes you will be leaving mobile users stranded in the mobile interface, with a “turn mobile off” button that doesn’t work. That said, if you have any mismatches between user-agents in the variety of lists they can appear, that also can cause problems. Check this all works after you update any of WordPress, WP Touch, WP Total Cache.

How to do it

WARNING: When you edit this group, WP Turbo Cache is editing .htaccess files. It doesn’t not escape them automatically, it’s VERY easy to break your website, including your admin interface… Don’t attempt to do this without file access, access to apache error logs, and ideally SSH access. Backup before you start: Your WordPress files, your wordpress database and also the WPTC config (there’s a download button on the first page).

To enable this hack you’ll have to do two things, one create the user agent group that will get the mobile touch theme.

1) Define the WP Touch User Agent Group

What we’re doing here is defining a list of the agents that should be redirected to the alternative cache pool, that shows the mobile theme.

My current WP Touch plugin claims on the admin interface to support “Android, CUPCAKE, bada, blackberry 9800, blackberry9500, blackberry9520, blackberry9530, blackberry9550, dream, iPhone, iPod, incognito, s8000, webOS, webmate

The list in W3 Total Cache must match what WPTouch expects, if it doesn’t then you could well have mobile served to desktops, and vice versa. The nicest way of doing this would be if your mobile theme was in fact a standalone theme, as W3TC can serve different themes to different User Agent Groups, and you’d only be doing the switch once. This isn’t possible though.

Select “User Agent Groups” from the dropdown in the W3TC admin panel

Copying, pasting and editing from the existing User Agent groups, I created a group with the following. When you save this page, you can easily cause 500 errors on your site, until you manually edit/remove various .htaccess files in your website directory. If this happens you’ll need to read the apache error log to track down the offending files.

android
blackberry9500
blackberry9520
blackberry9530
blackberry9550
blackberry9800
cupcake
dream
incognito
iphone
ipod
samsung\-s8000
webos
webmate

Delete the other groups that WP Total Cache has put in place by default. That keeps the set of mod_rewrite rules used by Apache smaller.

2) Add the cookie exemption

On the Page Cache Settings, add “wptouch_switch_toggle” (which used to be “wptouch_switch_cookie”) to the rejected cookies list.

Save, Deploy & Flush all the caches and test

After you’ve done that, you’re good to go. Apply the config, purge all your caches, then try to access the site on a “clean” desktop browser. Verify that you’re getting the desktop version, and the comments in the source show you’re being page-cached. (compare to a browser where you’re logged in where you should see something about ‘cookie rejected’)

The try on your nearest iPod/iPhone etc. You may will want to install a Javascript source viewer to debug and verify you’re indeed getting a cached copy, then comparing what happens as soon as you turn the mobile version off. You should the desktop site, with the “change to mobile” button. Expect to do a lot of user-agent faking, and cookie clearing to verify you’re really seeing what you should be getting.

Finally, hit reload lots, and if you’re cached, your server shouldn’t really notice and speed should be quite fast.

At this point you’ll also want to ask a few friends just to see what they are seeing on the site, just to check for consistency.

Governments can’t win on science advice

Science doesn’t always make common-sense, and that puts governments at a permanent risk of accusations of not giving the public the help they feel they deserve.

Updated for accuracy: current seasonal flu includes swine flu.

Last year we had the furore over Science and Drugs, that flared up again recently.

Most people I suspect, when asked, probably want policy to be evidence based.

However, today, the govt is under attack from the Labour opposition about the withdrawal of Flu vaccines for children under five. I’ll caveat that I’ve not read the research, and even if I was am unlikely to be in a position to appraise it. The attack seems to have arisen from the situation the under-5’s were recommended for H1N1 swine flu vaccine as they were more likely to experience complications. The current seasonal flu vaccines included H1N1 as one of the strains, and the majority of complications/deaths have arisen from H1N1. However, the fact that the media hook onto stories like this shows that really, “the public want science led policy, when it’s what they believe in”.

Epidemiological studies sometimes lead to advice that people don’t like, we don’t screen for all illnesses in all age-groups because for some of these groups the impact of testing outweighs the benefit. New treatments for cancer, always emotive when they are denied, are assessed on evidence of significant improvement over existing treatments. These are all too easily spun as “cost cutting is costing lives”, but if you want science led policy, somethings that won’t appear as tabloid friendly “common sense”.

I’m not a supporter of this government and I don’t think they should have axed the TV campaign for the vaccine – but when the science led policy isn’t what the public want to believe, all governments are vulnerable to allegations against them that the science is just an excuse for cost-cutting.

The Months After Everyone Else Kindle Review

I got a Kindle for Christmas, and months after everyone else got one, I write about what I like and what I don’t.

I got a Kindle for Christmas from my lovely parents.

Why I like it:

  • Form factor, screen size, weight, battery life
  • Reading more long form copy in a while, I think because I can get the right amount of copy on screen to match my natural skimming style
  • The inherent task switched that you have by picking it up, and the single-tasking of it. As someone else said (not that I can find the link) the web-browser will be useful for emergencies, but that’s about it. No push alerts, growls, or games to distract
  • Instapaper’s integration is really lovely (and finally helps me address the “popping” of To-Read items instead of the the “pushing” of them onto the stack)

Things I don’t like:

  • Limited choice of Newspapers – I would pay for the Guardian on it if I could. With the 3G variant, it’s a tablet thing always up-to-date with that.
  • Similar to that, I will not pay for the economist again. I don’t have to pay for the Online access, the iPhone app or website over and above my subscription, so much as I would love the have the economist on my Kindle – until Amazon/Publishers sort out a discount for subscribers I’m not
  • I can’t think how to do it efficiently, but the screensaver could be so much more than just a book image… (that said that completely breaks my previous statement that I like the mono-purpose of it)

I’m not going to pretend whose commercial teams are at fault here for these, but they are the main gripes I’ve found so far. Given those are policy, rather than technology, I hope they’ll shake out in time.

A Hunt related suggestion for styleguide writers

A family friend provides a solution to spoonerisms.

Many years ago a friend of the family got divorced: “Alan and Julie” were longstanding friends, and when “Alan” got into another relationship, we found it difficult to say “Alan” without “And Julie”.

We flipped them, it was always “Sue and Alan”.

In the light of recent naughtieness, can I suggest it’s always “Culture Secretary, Jeremy Hunt”.

In defence of new style RTs

a defence of new-style re-tweets on twitter.

This article talks about how “old-style” non-API retweets are where it’s at, and that you shouldn’t use the new ones. I couldn’t object more.

While I get that you can’t add commentary when you do an API-retweet, I’d argue that not using them is rude:

  • I can’t mute you if you RT too much, which I can with API retweets. (Good luck though in winning that meal in a mid-market chain restaurant)
  • My stream gets ‘clogged’ if multiple people I follow RT the same thing; with the API RT, I only see it once
  • That old-style RT you just did of a private account, thanks for ‘leaking’ that; new style RTs won’t let you RT something that’s private
  • By the time 3 people have RT’d something the old way, the URL at the end is truncated and doesn’t go anywhere, and you can’t easily get back to the original tweet

Seriously, not using new style-RTs is just impolite, and if you’re impolite people are going to stop following you.

The lounge is now a minefield

As we connect more devices in the lounge, we seem to be increasing the complexity of every interaction in what should be a space all about “lean back”

Disclosure: I work on YouView and this post is talking about personal experiences of connected TV devices (including competitors to my employer). Views are mine and not those of my employer.

I’ve recently moved house, and it’s only taken 1 month to get provisioned with wired internet. What did we ever do before we had 3G dongles to tied us over, and smartphones to lessen the connectivity gap? Anyway, I’ve got my internet, I’ve got a Samsung TV that runs apps, and an Apple TV.

The result? My lounge is confusing. I can watch YouTube on my Laptop, my TV, my iPhone or the AppleTV. I have to decide the “how” before the what. The TV does Lovefilm, the Apple TV does iTunes (which has taken more movie rental money than I’ve paid in years on the device that “doesn’t have much content”)

I can control my Apple TV from my iPhone, play iTunes content from my laptop on Apple TV, or pull content on the TV itself via DNLA from my laptop. I can twitter from my TV. Not with anything useful like what I’m watching, but I can multi-tap or t9 my thoughts onto the big screen.

Most of my interactions leave me thinking that second-screen is where it’s at. Tapping away into my iPhone doesn’t distract from the screen, and that’s vital in a shared environment like a lounge.

Finally, as a result of changing routers and network changes I actually said “Yeah, my TV doesn’t cope very well with renewing DHCP”. I almost choked having said that, because it’s not the kind of thing that you should ever say about a TV. TV should be reliable, simple. It’s the device to turn your brain off of an evening, not to engage in hand-to-hand network debug just to get things to work.

Connected TV is where things get interesting: we’ve just got to make them genuinely interesting, and not like ‘interesting’ problems your maths teacher gave you.

The power of language

Royal Mail infuriates with badly phrased letter.

I’ve recently moved house. Setting up mail redirection has been a bit of a faff because the postcode database doesn’t quite match reality, so I had to apply in person at a post office. This also required brandishing paper documents, which I don’t have because I mostly bank electronically. Anyway, a week later I go back and and apply. I’m told it’ll start on the 1st November.

I get a letter at the new place, “We’ll start (…) on 4-November, as you requested.”

In reality the extra 3 days is not a big deal; but if you’re going to include copy that calls out the user intent, it had better be real, and not “this is the date arrived at after we lost your form in the post”.

Getting Playful

Playful is a conference about games, and playfulness.

Another Friday, another conference. This time it was Playful 2010. Not as good as dConstruct but a fun day nonetheless.

I didn’t take many notes, but the general theme seemed to be “stop putting points and prizes on things, because that doesn’t make them a game, and it doesn’t make them fun”. This was perhaps seen as an about turn.

Richard Hogg and Sebastian Deterding were my favourite speakers. Richard did a great skit on being a contrarian and how you can “Escape from the crushing inevitability of liking something that’s good”. Sebastian was the dry German who pointed out that just because you’ve nicked badges from foursquare doesn’t make badges good or appropriate for your thing.

In between these things, some important 4square rules were sorted out: REALLY DON’T auto-tweet your location all the time. Especially if you’re a drive-by checkin merchant.

dConstruct 2010 in summary

A summary of the speakers at dConstruct 2010.

I attended my first dConstruct yesterday, which was a nice trip to Brighton, seeing friends and drinking a bit too much. My very brief take on the sessions:

The Designful Company
You’re aiming for different but good, but it takes guts to aim for and will test badly as it’s unfamiliar. Sticking with the familiar will test better, but ultimately market badly.

Boil, Simmer, Reduce
Collect ideas, play with them without pre-conceptions “Nobody will die doing this”, prune stuff back again, try and aim for simplicity.

Information is Beautiful
There’s too much information, this needs to be pruned and presented. Don’t do circular diagrams.

The Power & Beauty of typography
Typefaces have emotions and these should be in keeping with the overall site/copy. Much like shoes can set off or kill an outfit. This wasn’t too well received too well by a bunch of men and women attendees who appeared to be of the “what’s wrong with 1 pair of shoes and helvetica” crowd.

The Auteur Theory of Design
Explored the idea that projects need one ultimate authority like the Film Director with Final Cut, and that person is a limiting function on the output, and can drag up or down the overall output.

Jam Session: What Improvisation Can Teach Us About Design
Improvisation can bring about your best ideas, works best within a framework when you’re riffing off other people, and your self-inhibitions are lowered when you do it; removing that self-censorship can lead to new things.

The Value of Ruins
An unexpected standout for me; archives are potentially amazing for the future, but as we turn off things like Geocities we’re potentially losing just as much information as previous civilisations did in fires and the like.

Everything The Network Touches
Eons ago a cunning road network provided the ability to carry messages really quickly, and that communication gave empires advantages. Now we’re potentially building the infrastructure for this kind of stuff in the online world, with Bathroom Scales that tweet. Every time devices get more connected information becomes increasingly contextualised and ever more useful. Winner of the Most OCD-ly amazing slide Deck Animations Award – they really were lovely.

Kerning, Orgasms & Those Goddamned Japanese Toothpicks
Nerds care about things that other people don’t. That’s fine, don’t expect them to, try and make stuff so they don’t. Never get complacent, useful feedback probably hurts. Put the “narcism of minor differences” aside to deliver