How to Display EXIF Metadata in Wordpress's Media Library

Here's a quick snippet that displays some key EXIF metadata fields for images in Wordpress's Media Library.

Posted by David Coleman on October 25, 2018

When you import images into Wordpress's Media Library, it does actually ingest some of the EXIF and IPTC data. But Wordpress doesn't, by default, do much with it or even show it. But the metadata is there, attached the images, and with some fiddling it is possible to access and use it. (It may or may not provide SEO benefits; more on that here.)

There are various plugins that can do this and a whole lot more (Media Library Assistant is a great place to start), but if you're just looking to display the EXIF metadata in your Media Library and don't want to install another plugin, you can use this snippet, which was originally posted by the folks at Kinsta.com.

It displays some of the key EXIF metadata fields in the individual image pages of the Media Library. Specifically, it shows shooting data such as aperture, camera model, image timestamp, focal length, ISO, shutter speed, and orientation.

It goes in your theme's functions.php. Use the usual caution when editing your functions.php file.

// Display EXIF Metadata Media Library editing screen

function media_hacks_attachment_fields_to_edit( $form_fields, $post ){
// get post mime type

$type = get_post_mime_type( $post->ID );

// get the attachment path
$attachment_path = get_attached_file( $post->ID );

// get image metadata
$metadata = wp_read_image_metadata( $attachment_path );

if( 'image/jpeg' == $type ){

if( $metadata ) {

$exif_data = array(
'aperture'          => 'Aperture', 
'camera'            => 'Camera', 
'created_timestamp' => 'Timestamp',
'focal_length'      => 'Focal Length', 
'iso'               => 'ISO', 
'shutter_speed'     => 'Shutter Speed', 
'orientation'       => 'Orientation' );

foreach ( $exif_data as $key => $value ) {

$exif = $metadata[$key];
$form_fields[$key] = array(
'value' => $exif ? $exif : '',
'label' => __( $value ),
'input' => 'html',
'html'  => "<input type='text' class='text' readonly='readonly' name='attachments[$post->ID][$exif]' value='" . $exif . "' /><br />" 
);
}
}
}
    return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 'media_hacks_attachment_fields_to_edit', 10, 2 );

Things to Watch For

The first thing to watch out for is that getting something wrong in your functions.php file can knock your whole site offline. So exercise caution and create a backup of the file before you start. That way, if the site goes down with a white screen, you should be able to get things back up and working quickly simply by restoring the original functions.php file.

The second thing that isn't so much something to watch for but is something to be aware of, is that if you use an image optimization plugin or service, many of them strip out the image metadata as part of the upload process. Some provide an option to retain the metadata. Or if you've exported images from something like Lightroom and used the "Copyright Only" setting in the export dialog, that's also going to strip it out. But if the metadata has already been removed, these fields are going to end up displaying blank entries or 0.