Quick & dirty fix for Slimbox to attach itself to all images

While implementing yet another a WordPress site, I found myself in the situation where I wanted to display any and all images in lightboxes, but with a minimum of interaction from the editor’s part.

All current plugins that integrate Slimbox (that I know of – this one, for example) actually require the editor to add the rel="lightbox" attribute. But since the editors of this site have no HTML knowledge whatsoever and will be editing in “visual” mode, this hinders deployment.

So I came up with a modification to the actual javascript of Slimbox, to make it identify all links to images and lightboxify them :)

Here’s the actual code modification (starting at line 22 – on Slimbox version 1.41):

$each(document.links, function(el) {
if (el.href && el.href.test(/.(jpg|jpeg|png|gif|bmp)$/i)) {
el.onclick = this.click.pass(el, this);
el.rel = 'lightbox';
this.anchors.push(el);
}
}, this);
It replaces this:
$each(document.links, function(el){
if (el.rel && el.rel.test(/^lightbox/i)){
el.onclick = this.click.pass(el, this);
this.anchors.push(el);
}
}, this);

This replaces the detection of links that have “lightbox” in the rel attribute with a detection of all links that lead to images.

Although it suits my intended purpose, there are several drawbacks to this method that I can think of:

  • if there already is a rel attribute, it overwrites it
  • it won’t take advantage of the “gallery” feature

As mentioned in the title, this is a “quick & dirty” fix, but might help some.

If I get around, I’ll be writing a WordPress plugin with options to lightboxify all images, make a gallery of all images in a post / page (by filtering the_content() and adding a rel="lightbox[$post->ID]" to image links). Maybe even make a TinyMCE plugin that would add a button to lightboxify a certain image – right from the visual editor.

Article Info

This article was read 4477 times.

All scripts on this site are free. However, if you feel you want to contribute, use the button below.

COMMENTS (One comment)

I can’t understand why people don’t you use an additional class to mark an item for Lightbox/Slimebox/whatEverBox instead of rel.

This way, one can have several classes on the same element…

1 Tudor / February 3rd, 2009, 12:09 / #

Post a comment

Articles in the category

jQuery BBQ: Better Blockquotes

jQuery BBQ: A blockquote (AKA pullquote) script, that will unobtrusively grab content from your webpage and display it similarly to the way newspapers do it (see Wikipedia's definition of a pull quote). It can either be dropped directly into the webpage, with no configuration, or can take a number of customization parameters if you want [...]

SkinShare tutorial for an age verification using MOOdalBox

MOOdalBox has the the honor to be the first featured post on the SkinShare dev blog, with a tutorial on how to make a quick and easy age verification procedure. Unfortunately, this is a JavaScript only method – so it’s easily bypassed. I’ll have a version that uses more server-side scripting ready some time soon [...]

Tutorial for an AJAX login using MOOdalBox

While browsing the e-magine forums, answering a topic here and there, I discovered very well written and simple to follow tutorial on bulding a modal login dialogue using MOOdalBox. I’m really happy to see such a piece of work – it makes the time I spent writing the scripts even more meaningful. Thank you, Jani [...]

Vertical MooTools Kwicks (Fx.Elements), using relative sizes

I started out to make a vertical menu using Fx.Elements (instead of the classic horizontal one), for one of the sites I’m working on and, as always, I chose to use relatively sized elements (namely ‘em’). However, although Fx.Base has support for specifying the unit, the original Kwicks demo doesn’t play well out of the [...]