,

Automatically add EXIF information to photos

Share this:

I was looking for an easy and automated way to show EXIF information under images I upload, especially from the mobile app. A super useful article on WPBeginner helped me figure it out, just needed a plugin and a code snippet. Below are a few test images I uploaded to check if it worked as intended.

I’ve modified the provided code snippet slightly because I wanted to display the lens being used and focal length the photo was captured at. Have shared it at the bottom of this post.

Camera:OM-1MarkII Lens:OM 150-600mm F5.0-6.3 Focal length:600/1 F-number:f/6.3 Shutter speed:1/640 ISO:1250 Date:2026-09-11 06:57:01
Camera:Xiaomi 14 F-number:f/1.6 Shutter speed:1069/714955 ISO:50 Date:2026-09-11 06:48:47
Camera:OM-1MarkII F-number:f/6.3 Shutter speed:1/2000 ISO:2500 Date:2026-09-06 07:15:08
Camera:OM-1MarkII F-number:f/8.0 Shutter speed:1/250 ISO:200 Date:2026-09-11 07:02:15
Camera:OM-1MarkII F-number:f/6.3 Shutter speed:1/160 ISO:12800 Date:2026-09-06 09:46:26
Camera:Xiaomi 14 F-number:f/2.0 Shutter speed:347/395094 ISO:50 Date:2026-08-31 15:08:30

Here’s the code snippet i’ve customised for use along with the EXIF Details plugin:

function exif_details_change( $exifdatas, $id ) {
    if ( array_key_exists( 'DateTimeOriginal', $exifdatas ) ) {
        $shooting_date = str_replace( ':', '-', substr( $exifdatas['DateTimeOriginal'], 0, 10 ) );
        $shooting_time = substr( $exifdatas['DateTimeOriginal'], 10 );
        $exifdatas['DateTimeOriginal'] = $shooting_date . $shooting_time;
    }
    return $exifdatas;
}
add_filter( 'exif_details_data', 'exif_details_change', 10, 2 );
 
function media_caption( $metadata, $id ) {
    $mime_type = get_post_mime_type( $id );
    if ( in_array( $mime_type, array( 'image/jpeg', 'image/tiff' ) ) ) {
        do_action( 'exif_details_update', $id );
        $exifdatas = get_post_meta( $id, '_exif_details', true );
        if ( ! empty( $exifdatas ) ) {
            $camera = null;
			$lens = null;
			$focal = null;
            $f_number = null;
            $s_speed = null;
            $iso = null;
            $date = null;
            $googlemap = null;
            if ( array_key_exists( 'Model', $exifdatas ) ) {
                $camera = 'Camera:' . $exifdatas['Model'];
            }
			if ( array_key_exists( 'UndefinedTag:0xA434', $exifdatas ) ) {
                $lens = 'Lens:' . $exifdatas['UndefinedTag:0xA434'];
            }
			if ( array_key_exists( 'FocalLength', $exifdatas ) ) {
                $focal = 'Focal length:' . $exifdatas['FocalLength'];
            }
            if ( array_key_exists( 'ApertureFNumber', $exifdatas ) ) {
                $f_number = 'F-number:' . $exifdatas['ApertureFNumber'];
            }
            if ( array_key_exists( 'ExposureTime', $exifdatas ) ) {
                $s_speed = 'Shutter speed:' . $exifdatas['ExposureTime'];
            }
            if ( array_key_exists( 'ISOSpeedRatings', $exifdatas ) ) {
                $isodata = json_decode( $exifdatas['ISOSpeedRatings'] );
                if ( is_array( $isodata ) ) {
                    $iso = 'ISO:' . $isodata[0];
                } else {
                    $iso = 'ISO:' . $isodata;
                }
            }
            if ( array_key_exists( 'DateTimeOriginal', $exifdatas ) ) {
                $date = 'Date:' . $exifdatas['DateTimeOriginal'];
            }
            if ( array_key_exists( 'latitude_dd', $exifdatas ) && array_key_exists( 'longtitude_dd', $exifdatas ) ) {
                $googlemap = '<a href="https://www.google.com/maps?q=' . $exifdatas['latitude_dd'] . ',' . $exifdatas['longtitude_dd'] . '">Google Map</a>';
            }
            $caption = sprintf( '%1$s %2$s %3$s %4$s %5$s %6$s %7$s %8$s', $camera, $lens, $focal, $f_number, $s_speed, $iso, $date, $googlemap );
            $caption = rtrim( $caption );
            $caption = preg_replace( '/\s(?=\s)/', '', $caption );
            $media_post = array(
                'ID'           => $id,
                'post_excerpt' => $caption,
            );
            wp_update_post( $media_post );
        }
    }
    return $metadata;
}
add_filter( 'wp_generate_attachment_metadata', 'media_caption', 10, 2 );
Share this: