Getting PicLens to play nicely with WordPress pretty permalinks

When the nice lady on the BBC showed me the PicLens plugin, I knew my search for a cool way to show visitors my photos was finally over. I always liked Lightbox and its variants, but I wanted something that would allow people to easily browse the pictures and show them full screen with no distracting clutter. PicLens does all that, and in a very nice way. Even when you don’t have the plugin (and there isn’t one for Opera, for instance) or Flash installed you still get a pretty nice JavaScript alternative.

They provide easy ways to integrate PicLens into your site, including the wp-piclens plugin for WordPress. But there’s a snag. Out of the box, at least for me with my WordPress 2.3.3 installation, it only works properly when pretty permalinks turned off. (That means style links would work, but not my preferred ones). The way it would fail is that when you click on the “Start Slide Show with PicLens Lite” link which the plugin helpfully inserts into posts which have images in them, PicLens opens up but sits there forever with a “loading” progress bar, not getting anywhere.

Time to break out the debugging chops :->

It turns out that PicLens was actually getting a 404 error when it requested the Media RSS feed from wp-content/plugins/wp-piclens/mrss.php. Weird, since when you request the feed directly, you’d be confused to see that the feed actually appears – but sure enough, look at the headers with Live HTTP Headers, and the response code is a nasty 404 instead of the nice 200 we might expect. This is caused by the way that wp-piclens invokes the wp() function to generate the MediaRSS. To cut a long story short, the WordPress parse_request function assumes that when URI rewriting is turned on, it’s going to generate a 404 error. Then, if and only if it finds a rewrite rule to successfully apply, it resets that error. So, despite the fact that the URI for the MediaRSS feed doesn’t actually need rewriting, you get a 404 error when you request it when URI rewriting is turned on. The fix? Add a hook in the plugin to rewrite the mrss.php URI onto itself, like this:

function feed_dir_rewrite($wp_rewrite)
$feed_rules = array( 'wp-content/plugins/wp-piclens/mrss.php' => 'wp-content/plugins/wp-piclens/mrss.php' );
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
add_filter('generate_rewrite_rules', 'feed_dir_rewrite');

Then you need to force the rules to be re-generated: go to Options/Permalinks in your WordPress dashboard, select “Default” and hit “Update Permalink Structure”, then re-select your preferred permalink method (mine is “Date and name based”) followed by another “Update Permalink Structure”.

That should be it! Now when you click on the “Start Slide Show with PicLens Lite” link, you should see your photos on the PicLens wall in all their glory.

(Now I can finally get on with adding some content instead of fiddling with infrastructure… maybe…)

One Response to “Getting PicLens to play nicely with WordPress pretty permalinks”

  1. Jon Says:

    Aw, crud. There’s nothing like prattling on about how great you are when in fact the problem’s far simpler than you thought. All I needed, in fact, was to get my URI rewriting straight since my WordPress blog sits in /wordpress but I want the URIs to be relative to the root. Nothing unusual there, except I also want other sub-sections of the site like /footy to work too. So it was just a case of adding soft links from the root to /wordpress for wp-admin, wp-content and wp-include. Never mind.