WordPress: get featured image exif data

Whilst redesigning my blog this year, a big part of it was to create a proper photoblog section, but none of the photoblogging plugins quite fit what I wanted to do, so I simply created a photoblog category and intend to post photos into that section when I can.

Part of this section was to make sure that there was a bit of additional information about the image. Fortunately, as long as you’re not minimising your embedded metadata on the image, WordPress collects all of this information.

The inspiration for this functionality came from Walkernews.net’s post “Turn On WordPress Feature To Display Photo EXIF Data and IPTC Information“. I wouldn’t have had a clue how to get display the correct shutter speed information otherwise!

That post focused on editing your image.php template file to display EXIF data. I’m using a lightbox which precludes the need for the image attachment template file, and I wanted to output a definition list showing the important EXIF information pertaining to the featured image of each photoblog post.

The Function

/**
 * Create a definition list containing EXIF data of featured image (if exists)
 *
 * @param	string		$post ID
 * @return 	echo definition list
 */
function pa_the_post_thumbnail_exif_data($postID = NULL) {
	// if $postID not specified, then get global post and assign ID
	if (!$postID) {
		global $post;
		$postID = $post->ID;
	}
	if (has_post_thumbnail($postID)) {
		// get the meta data from the featured image
		$postThumbnailID = get_post_thumbnail_id( $postID );
		$photoMeta = wp_get_attachment_metadata( $postThumbnailID );

		// if the shutter speed is not equal to 0
		if ($photoMeta['image_meta']['shutter_speed'] != 0) {

			// Convert the shutter speed to a fraction
			if ((1 / $photoMeta['image_meta']['shutter_speed']) > 1) {
				if ((number_format((1 / $photoMeta['image_meta']['shutter_speed']), 1)) == 1.3
				or number_format((1 / $photoMeta['image_meta']['shutter_speed']), 1) == 1.5
				or number_format((1 / $photoMeta['image_meta']['shutter_speed']), 1) == 1.6
				or number_format((1 / $photoMeta['image_meta']['shutter_speed']), 1) == 2.5) {
					$photoShutterSpeed = "1/" . number_format((1 / $photoMeta['image_meta']['shutter_speed']), 1, '.', '') . " second";
				} else {
					$photoShutterSpeed = "1/" . number_format((1 / $photoMeta['image_meta']['shutter_speed']), 0, '.', '') . " second";
				}
			} else {
				$photoShutterSpeed = $photoMeta['image_meta']['shutter_speed'] . " seconds";
			}
			// print our definition list
		?>
			<dl>
				<dt>Date Taken</dt>
				<dd><?php echo date("d M Y, H:i:s", $photoMeta['image_meta']['created_timestamp']); ?></dd>
				<dt>Camera</dt>
				<dd><?php echo $photoMeta['image_meta']['camera']; ?></dd>
				<dt>Focal Length</dt>
				<dd><?php echo $photoMeta['image_meta']['focal_length']; ?>mm</dd>
				<dt>Aperture</dt>
				<dd>f/<?php echo $photoMeta['image_meta']['aperture']; ?></dd>
				<dt>ISO</dt>
				<dd><?php echo $photoMeta['image_meta']['iso']; ?></dd>
				<dt>Shutter Speed</dt>
				<dd><?php echo $photoShutterSpeed; ?></dd>
			</dl>
		<?php
		// if shutter speed exif is 0 then echo error message
		} else {
			echo '<p>EXIF data not found</p>';
		}
	// if no featured image, echo error message
	} else {
		echo '<p>Featured image not found</p>';
	}
}

Please note

I’m working off the assumption that if there’s no shutter speed meta EXIF information, then the image probably doesn’t have any of the EXIF information and so we just display an error message. Conversely, I’m also assuming that if the shutter speed is listed, then all the other relevant EXIF data will be there. Feel free to add more checks!

In my case I didn’t need copyright, title, caption, or credit information, but you can easily add these in by calling the following.

echo $photoMeta['image_meta']['copyright'];
echo $photoMeta['image_meta']['title'];
echo $photoMeta['image_meta']['caption'];
echo $photoMeta['image_meta']['credit'];

Usage

I’m using this in single.php, within the loop. You can use it outside the loop if you specify the $post->ID. As it’s in my single.php but I only want it to show up in my photoblog category I’m using a conditional query before any output.

<?php if (in_category(3)) {    ?>
<h3>EXIF Data</h3>
<?php pa_the_post_thumbnail_exif_data(); ?>
<?php } // end if in_category(3); ?>

Conclusion

Hopefully this offers a fairly solid option for including EXIF data in your posts. You can check it out over in my photoblog section.

If you can think of a better option, please let me know in the comments below. If it worked for your site, drop me a comment so I can see it in action!

Comments

  • Guido

    hey can you extend this great snippet with some y and x coordinates to implement a link to e.g. google maps?

    • Hey Guido,

      I haven’t been tagging geo-coordinates to my photos as of yet so I’m not 100% sure if wp_get_attachment_metadata() provides that information. 

      You’ll need to do a test – assign wp_get_attachment_metadata() to a variable and print_r it. If one of the entries relates to geo-coordinates then it should be very easy to pull that information out and place a marker on a map. Probably best to do this in your header.php so if the geo-coordinates exist you can populate a script which gets Google Maps API v3 to dynamically populate a div with map and marker.

      It’s a good idea and I’ll do some tests myself but I won’t be able to find time to work on that for a while.

      If the attachment metadata doesn’t include geo-coordinates I’d suggest just doing it manually with a great plugin like http://pronamic.eu/wp-plugins/google-maps/ which makes it super easy to drop a marker when creating the post and populate a map on the front end.

  • CtrlSPATIE

    Lovely post, exactly the snippets that helped me solve the things I needed to solve.

    🙂

    Thanks.

  • ridetorio

    You’re a genius, thank you 🙂

  • ridetorio

    Hey Alex, i was wondering if theres an easy way to extend this so that it affects any images uploaded to a post, not just the featured image? Thanks so much!

    • Hey Ridetorio

      After a hectic week of work I was just looking for your original comment and was confused when I couldn’t find it – then I spotted the edit 🙂

      You got it sorted? Should just be a matter of looping through post_attachments. Hope it didn’t take long to work out and sorry for the delay replying!

  • Adi Pranadipa

    Ahh. just browsing around and..i found what i needed in this tuts, in my case , i’m still in developing website for my community of photographers with wordpress. I think this tutorial are right references for the project, but I want to separate photos into Custom Post Type of Photos, would you mind to share any tricks that could help..?

    • Hey Adi,

      Certainly a custom post type can give you a lot more control over things. Advanced Custom Fields is one plugin I highly recommend for being easily able to further customise a post type.

  • David Gray

    Works like a charm with 4.1.1. Thanks for all your hard work on this!