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”);
?>

5 Comments
[...] 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. [...]
October 26th, 2006 at 3:06 pm. Permalink.
Nice !
But I use a plugin called wp-lightbox, which also does the same thing I guess
October 27th, 2006 at 7:36 pm. Permalink.
Funnily enough, I brought this to Will attention about midday today.
“It doesn’t automagically do it though”, he claimed. Until he actually read the page
October 27th, 2006 at 8:21 pm. Permalink.
….I updated the post before your comment
October 28th, 2006 at 10:51 am. Permalink.
[...] Not to be outdone by Will, I started investigating some neato AJAXy stuff I’d read about awhile ago. [...]
October 28th, 2006 at 4:10 pm. Permalink.