The release of WordPress 2.5 included a built in gallery feature. Nothing major by any means, but it gave you the option of using a simple gallery to display your photos. It also included the ability to pull the EXIF data from your photos. However, whilst the features are there, there’s not much information or tutorials on using them. Hopefully this post will give you a bit more information on how to get the most from this new feature.
Create the Gallery
You can create the gallery in a post or a page. If you want to create a collection of galleries then you need to decide whether you’re going to use a static Page as the page parent, and then for each gallery you create a child page. Alternatively, you can create a Gallery category and then each gallery is a post within this category. Either method is fine, but for this example I’ll assume you’re going the post route.
So, you’ve got your category called Gallery (or whatever you wish to call in). Create a new post, giving it a title as usual. You can add in some content too if you like. Then click on the ‘Add an Image’ button by Add media in the top right of the post area. A popup should appear allowing you to upload your photos. You can do them individually or several at a time (note you may need to change the file permissions on your wp-content/uploads folder for this to work). Once they’ve uploaded, click on the Gallery tab at the top of the box. You can then go through each photo by clicking the Show link, and give each photo a title, caption and description. Once you’re done, click Save all Changes. Then click ‘Insert Gallery into Post’.
Your post will now have the gallery shortcode placed where the cursor was – [gallery]. You can easily move this if necessary. The gallery shortcode can accept a few options/parameters:
- Specifies how many columns the gallery should span. The default is 3.
- Controls which size to display on the gallery page. The default is thumbnail, the options are thumbnail, medium and full.
Other options can be read about on the Gallery Shortcode codex page. To use these options you add them into the gallery shortcode eg.
[gallery columns="2" size="medium"]
Once you save this post and view it you should see the selected size photo for each photo uploaded, laid out in a simple gallery format, with the caption below each image. Clicking on the image opens up the medium version of the image and displays the image title above the image and the description given below it. This single image display page uses the single.php theme file. You can also use image.php or application.php, however I found the image disappeared with these!
To control the size of the thumbnail and medium size images go to Settings -> Miscellaneous in the WordPress Admin and change the values for the images there.
The EXIF Data
When you upload your photos the EXIF data is stored in the post meta data table. Information on how to extract this and display it is very thin on the ground, so if there's a better method feel free to leave a comment, however the following works and should be futureproof.
To extract the complete field into a variable (which will then store as an associative, multi dimensional array) we use the following line of code within the loop (ideally perhaps after the_content() tag)
$imgmeta = wp_get_attachment_metadata( $id );
The $id is the attachment ID and is created by the loop so this is already set. There is a variety of data that you can then echo out:
- The width of the original photo uploaded.
- The height of the original photo uploaded.
- The aperture used.
- The person credited for taking the photo.
- The camera used.
- The timestamp of when the photo was taken.
- The copyright on the photo.
- The focal length used in mm.
- The ISO used
- The shutter speed used in seconds.
- The title given to the photo.
- The caption given to the photo.
Some of this data will not be available without it being added via a program such as Photoshop. Other additional data may be available depending on your camera’s settings (I’ll continue to add to this list as I find more details available).
So we can take what we want from this data and display it with the photo. A couple of items will need formatting, for example the timestamp is in seconds so we need to convert it to a normal date and time using PHP, and the shutter speed will need rounding off to a couple of decimal places. We also only want this displayed if we’re displaying an image. So a final list could look like
[sourcecode language=”php”]< ?php
if (is_attachment()) :
$imgmeta = wp_get_attachment_metadata( $id );
- Dimensions: ” . $imgmeta[‘width’].” x “.$imgmeta[‘height’].”
- Aperture: f/” . $imgmeta[‘image_meta’][‘aperture’].”
- Camera: ” . $imgmeta[‘image_meta’][‘camera’].”
- Date Taken: ” . date(“d-m-Y H:i”, $imgmeta[‘image_meta’][‘created_timestamp’]).”
- Copyright: ” . $imgmeta[‘image_meta’][‘copyright’].”
- Focal Length: ” . $imgmeta[‘image_meta’][‘focal_length’].”mm
- ISO: ” . $imgmeta[‘image_meta’][‘iso’].”
- Shutter Speed: ” . number_format($imgmeta[‘image_meta’][‘shutter_speed’],2).” seconds
- Date Taken: ” . date(“m-d-Y H:i”, $imgmeta[‘image_meta’][‘created_timestamp’]).”
For US format for the timestamp use the following:
[sourcecode language=”php”]echo “
For more options for this see the PHP Date reference.
This new feature should allow you to use WordPress as a standalone photo gallery or as part of a bigger site, without the need for plugins. Of course the various photo gallery plugins out there will give you a lot more options, but with a bit of thought, this gallery can be quite powerful. Using the post method means your galleries index is automatically updated with the latest gallery at the top, just like a usual blog category and blog post format.
The best (and really only) use I’ve seen of this new feature is from Matt Mullenweg’s Gallery. Whether he’s manually put in the photo count, used a bit of PHP to query the number of images in the gallery (perhaps there’s a template tag for that which I’ve not come across yet?), or it’s a potential future feature, who knows. But it’s a nice use and goes to show the potential for what this gallery has.
(Code not working? Be sure to remove the additional space between the <? and php. It’s added by the code plugin unfortunately)