Custom Gallery Shortcode Plugin

As I've been working recently on fixing all of the broken image galleries on Leovanna I came across a situation in which the built-in [gallery] shortcode was not sufficient.

The Problem

To highlight the problem, it's worth seeing what the final result looks like, and work backwards. The page on Keira looks simple enough - a couple of larger pics at the top, with a gallery of thumbnails underneath. WordPress users won't see anything unusual here except that the gallery does not contain the two larger images in its collection of thumbnails!

What I needed, was some way of using the [gallery] shortcode, but being able to exclude certain images. Step up Michael Fields and his Custom Gallery Shortcode WordPress plugin (which he's now deprecated as WP natively provides most of the same functionality).

The Solution

The plugin itself is straight forward - it simply adds two new attributes to the [gallery] shortcode, include and exclude.

[gallery include="789,790,791"]
[gallery exclude="672, 673"]

Of use to me in the Leovanna pages, was the exclude attribute. It meant I could display the thumbnail gallery, but exclude the attachment ID of the two larger images and just insert them directly into the post with the native WordPress feature.

How to Find the Attachment ID

Getting the attachment ID from the status bar

Getting the attachment ID from the status bar

The hardest bit about using this plugin is actually an issue with WordPress itself, and which is crying out for a plugin itself. The attachment ID for an image is tucked away within the WordPress database; it's not visibly displayed on screen, either on the media pop-up screen for a post/page or on the media page. However, by going to the Media page, and hovering over an Edit link, you should be able to see the ID within the status bar of most common browsers. From the image shown here, the ID is 1002, so this is the value I would use in the comma-separated list of values for the include or exclude attribute.

Using the Include Attribute

Being able to miss out a select few images from a gallery seems ideal, so what use is the include attribute supported by this plugin? Again, lets see an example from Leovanna, this time on a page about the Keira / Logan Litter of puppies. As you can see, each puppy effectively has it's own little gallery (although, if you click on one to bring the enlarged version up, you still have the benefit of looping through each image on the page) and a few words or headings can be added in between. It's obviously easier to use include="1,2,3" than to use exclude followed by 20-30 attachment IDs.

Benefit to Thesis users

One of the benefits of using the Thesis theme for WordPress is that it has support for post image and thumbnails built in. There's two ways you can add these post images in - either upload via the Media page, and not have the image attached to a post, or upload it directly from within the post-writing screen, and attach it to the post in question. This latter method may mean then that an image used for marking a post on a Feature and Teasers page also then gets shown within a gallery on that page. If you don't want that, and don't want to have unattached images floating around in your WordPress database, then the Custom Gallery Shortcode plugin will help here.

Other Benefits of this plugin

It's long been known that the native gallery feature of WordPress spits out invalid code, but this plugin fixes that. The native feature also puts a <style> element just before each gallery itself, but this plugin moves this to it's rightful place in the head, and applies the CSS width statement via a style attribute.

One Caveat

Michael mentions the following on his plugin page:

All attachments link directly to themselves, bypassing attachment.php or any of its derivatives (image.php, pdf.php, etc…) There is no easy way to link to the attachment page while retaining the 'set' of images defined by "include" or exclude parameters.

What that simply means is, the gallery images will be linked to the file, not the attachment page. However, as most of the Lightbox variations (I prefer Shutter Reloaded) require that this needs to happen for them to be functional, then this isn't a major problem.

The Future of the Plugin

When I found this plugin, there were a couple of things that weren't quite right, but I found a solution and left comments on Michael's plugin page. Michael immediately took notice, applied the fixes (which, according to some other posters, should help them out too), and released a new version. I've since suggested some other features for the plugin, and Michael, via email has implied that these might be included in a future version.

Summary

This plugin is a perfect example of what plugins should be. It's one which adds such subtle yet necessary functionality to the WordPress system that you wonder why they never built-in into the core in the first place. I've urged Michael to push the core developers to add the obvious features that this plugin supports. In the meantime, if you use the [gallery] shortcode, and want to control exactly which images appear in the final post, then this plugin is an absolute must-have.

Speak Your Mind

*