Gallery of Doodles in Lightbox 2

Nerd­StarGamer now has a new Gallery page that fea­tures my doodles:

Screenshot of Doodle Gallery

All of the images have been set up as a list of thumb­nails which use Light­box 2 to dis­play large versions.

I spent a little extra time to set up Light­box on this blog with­out using a plugin. I’ve been on a steady cru­sade to get rid of most of my plu­g­ins for quite some time. Set­ting up Light­box in Word­Press was fairly straightforward.

After down­load­ing the Light­box 2 files, I cre­ated a new direc­tory in my tem­plate direc­tory called lightbox and dropped all of the light­box files into it. I then put a func­tion call into the header.php file right before the line that reads <?php wp_head(); ?>.

<head profile="http://gmpg.org/xfn/11">
    ...some other tags...

    <?php AKM_include_lightbox(); ?>
    <?php wp_head(); ?>
</head>

The AKM_includ_lightbox(); func­tion is just a short little func­tion that I wrote and put in the functions.php file of my tem­plate. Here is the function:

function AKM_include_lightbox() {
    $lbDir = get_bloginfo('template_directory') . "/lightbox";

    // Echo out some file path variables for images used lightbox JS
    $output = '<script type="text/javascript">' . "\n";
    $output .= "\t" . 'var tplDir = "' . $lbDir . '";' . "\n";
    $output .= '</script>' . "\n";

    // Echo links to js and css for lightbox
    $output .= '<script type="text/javascript" src="' . $lbDir . '/js/prototype.js"></script>' . "\n";
    $output .= '<script type="text/javascript" src="' . $lbDir . '/js/scriptaculous.js?load=effects,builder"></script>' . "\n";
    $output .= '<script type="text/javascript" src="' . $lbDir . '/js/lightbox.js"></script>' . "\n";
    $output .= '<link rel="stylesheet" href="' . $lbDir . '/css/lightbox.css" type="text/css" media="screen" />' . "\n";

    echo $output;   
}

This first line of the func­tion sets up a vari­able that includes the path to the Light­box files inside my tem­plate direc­tory. This is nec­es­sary because the lightbox.js file needs to ref­er­ence the images included in the Light­box folder. With­out this part, the pre­vi­ous, next and close images will not show up because the link will be going to your Word­Press uploads directory.

That second chunk of text in the func­tion echos out a small bit of JavaScript into your header that simply declares the vari­able tplDir and sets it to the path to your Light­Box instal­la­tion. The last chunk of text inserts all of the nec­es­sary Light­box JavaScript and CSS links into your header. I could have writ­ten all of this directly into the header.php file, of course, how­ever I felt that my file was get­ting a bit messy and that this approach was much more clear.

We also need to make a small edit to the lightbox.js file which is going to use that tplDir vari­able we set. Find the line in the begin­ning of the file like this (around line 49):

fileLoadingImage:        'images/loading.gif',     
fileBottomNavCloseImage: 'images/closelabel.gif',

Simply change those two lines to this:

fileLoadingImage:        tplDir+'/images/loading.gif',     
fileBottomNavCloseImage: tplDir+'/images/closelabel.gif',

That com­pletes the Light­box 2 setup in Word­Press with­out using a plugin. Now all you have to do is add the rel="lightbox" tag to any link you want to use Light­box. For exam­ple, if you have a thumb­nail image that links to a larger image like this:

<a href="images/full-size-image.jpg"><img src="images/thumbnail" /></a>

To add the Light­box effect, just add in the attribute like this:

<a rel="lightbox" href="images/full-size-image.jpg"><img src="images/thumbnail" /></a>

Be sure to check out the Light­box 2 page for more infor­ma­tion on what you can do with it.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Comments

1. Getting Geeky With YSlow | NerdStarGamer

[...] I first set up Light­box, I wanted to avoid using a Word­Press plugin for it, so I cooked up my own method of includ­ing it. Most of the work was simply trying to find a way around hard-​coding my tem­plate direc­tory in it [...]

Leave a Reply