Wordpress Plugin: Automatically Add Lightbox Code to Linked Images.

Posted in Coding, IT by Will on October 26, 2006.

Update:
As Venu points out below, and Paul pointed out to me earlier today, the Wordpress Plugin wp-lightbox will do this for you (plus install the Lightbox code for you).


With some pointers from Paul Jenkins, I wrote this plugin for Wordpress that will automaticly add Lightbox code to linked images. It’ll also go and copy the alt tag from the image as the Title for the URL - so Lightbox will show it as a caption.

It’s not fully tested yet (it’s on this site, though).

Situations where you might have some difficulty:
* Large linked images (i.e wider than the browser/screen dimensions)
* Images linked to non-images.

However, it’ll only add the lightbox code if there’s only an image wrapped in a link — it won’t add it otherwise. As a crude hack, you could insert a space, or anything else to break the insertion.

Additionally - it won’t add (or more correctly: will remove itself from) entries where you’ve manually added lightbox code or title values.

If you’ve got a better approach than the three pass filter - please let me know (I’m no regex guru).

Without further ado, here’s the code - save it to a .php file in your Wordpress plugins directory, and then activate.

<?php

/*
Plugin Name: Lightbox Auto-Add
Plugin URI: http://will.hughesfamily.net.au/20061026/wordpress-plugin-automatically-add-lightbox-code-to-linked-images/
Description: Automatically Adds lightbox code to linked images.
Version: 0.4
Author: William Hughes
*/

function lightbox_autofilter($content)
{

// three-pass filter... 

// first, go and add the lightbox code everywhere needed...
$content = preg_replace('/<a ([^<]*?)><img ([^<]*)(alt="([^"]*?)")([^<]*)><\/a>/i','<a rel="lightbox" title="$4" $1><img $2$3$5></a>',$content); 

// now remove any duplicate values that might have been added (eg: if lightbox code was already added by someone)
// purely for HTML nice-ness. 

$content = preg_replace('/<a rel="[^"]*?" ([^<]*?rel="[^"]*?"[^<]*?)>/i','<a $1>',$content);
$content = preg_replace('/<a ([^<]*?) title="[^"]*?" ([^<]*?rel="[^"]*?"[^<]*?)>/i','<a $1 $2>',$content); 

return $content;
}
add_filter("the_content","lightbox_autofilter");

?>

Windows Live Writer

Posted in IT by Will on October 26, 2006.

Update: I’ve now written a plugin that’ll automatically add the (basic) lightbox code for you in Wordpress itself.

Update 2: Use the Wordpress Plugin wp-lightbox to do all this (and more) for you automaticly.

Well, after a successful test just now, I have to agree with a lot of the commentary out there: Live Writer is actually really cool.

I’m not saying anything new by saying that someone needs to build a plugin for it to enable automagic Lightbox code embedding.

Sure, I can go and add the HTML manually (Yeah, Live Writer actually lets me do that easily enough), but given how slick the rest of the app is - it’s a shame, really that I need to do that.

The addition of drop shadows to the photo by default is interesting, but I’d prefer that it remembered my last selection (Photo paper, in this instance).

C’mon, don’t make me do it… *whine*

Oh, and yeah - you can customise the HTML, but that’s only specific elements - like URL, or Image, etc - not a combination of them.

 Meanwhile… here’s more photos (just ’cause I can)

 Spider, Spider on the Wall...

Scrams.org Gathering Photos

Posted in Food, Photography by Will on October 26, 2006.

This is really just an excuse to test out the Windows Live Writer thingy… seems  quite nice, acutally.

 

Some of the pictures I took from the recent Scrams.org meeting.

Satay Chicken Skewers

teriyaki Beef Skewers

Nori Rolls

Ben, from Feast Catering