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