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
				<dt>Date Taken</dt>
				<dd><?php echo date("d M Y, H:i:s", $photoMeta['image_meta']['created_timestamp']); ?></dd>
				<dd><?php echo $photoMeta['image_meta']['camera']; ?></dd>
				<dt>Focal Length</dt>
				<dd><?php echo $photoMeta['image_meta']['focal_length']; ?>mm</dd>
				<dd>f/<?php echo $photoMeta['image_meta']['aperture']; ?></dd>
				<dd><?php echo $photoMeta['image_meta']['iso']; ?></dd>
				<dt>Shutter Speed</dt>
				<dd><?php echo $photoShutterSpeed; ?></dd>
		// 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'];


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); ?>


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!