WordPress Gallery and EXIF

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

    echo “
  • Dimensions: ” . $imgmeta[‘width’].” x “.$imgmeta[‘height’].”
  • \n”;
    echo “

  • Aperture: f/” . $imgmeta[‘image_meta’][‘aperture’].”
  • \n”;
    echo “

  • Camera: ” . $imgmeta[‘image_meta’][‘camera’].”
  • \n”;
    echo “

  • Date Taken: ” . date(“d-m-Y H:i”, $imgmeta[‘image_meta’][‘created_timestamp’]).”
  • \n”;
    echo “

  • Copyright: ” . $imgmeta[‘image_meta’][‘copyright’].”
  • \n”;
    echo “

  • Focal Length: ” . $imgmeta[‘image_meta’][‘focal_length’].”mm
  • \n”;
    echo “

  • ISO: ” . $imgmeta[‘image_meta’][‘iso’].”
  • \n”;
    echo “

  • Shutter Speed: ” . number_format($imgmeta[‘image_meta’][‘shutter_speed’],2).” seconds
  • \n”;
    echo “”;

    For US format for the timestamp use the following:

    [sourcecode language=”php”]echo “

  • Date Taken: ” . date(“m-d-Y H:i”, $imgmeta[‘image_meta’][‘created_timestamp’]).”
  • \n”;[/sourcecode]

    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)

Disclosure: In full disclosure, it is safe to assume that the site owner is benefiting financially or otherwise from everything you click on, read, or look at while on my website. This is not to say that is the case with all content, as all publications on the site are original and written to provide value and references to our audience.


  1. Dean Saliba July 20, 2008
  2. Richard Bui July 20, 2008
  3. Richard Bui July 20, 2008
  4. Richard Bui July 20, 2008
  5. Michael July 20, 2008
  6. Sarah July 20, 2008
  7. Richard Bui July 20, 2008
  8. Sarah July 20, 2008
  9. Richard Bui July 21, 2008
  10. Codrut Turcanu I Rem July 21, 2008
  11. Elaine July 29, 2008
  12. Sarah July 29, 2008
  13. Elaine July 29, 2008
  14. Sarah July 29, 2008
  15. slee July 30, 2008
  16. Sarah July 30, 2008
  17. Richard Bui August 2, 2008
  18. Sarah August 3, 2008
  19. archshrk August 7, 2008
  20. Grant H August 8, 2008
  21. SarahG August 8, 2008
  22. archshrk August 11, 2008
  23. archshrk August 11, 2008
  24. Sarah August 12, 2008
  25. YouON August 18, 2008
  26. Sarah August 18, 2008
  27. YouON August 18, 2008
  28. Jack R August 20, 2008
  29. Tyler September 16, 2008
  30. Hannes September 23, 2008
  31. Sarah September 23, 2008
  32. Shane September 29, 2008
  33. Sarah September 29, 2008
  34. Shane September 30, 2008
  35. Sarah October 1, 2008
  36. Dreamcolor November 29, 2008
  37. Sarah November 30, 2008
  38. Ronny December 19, 2008
  39. Mihai Albu December 30, 2008
  40. dzu December 3, 2010