Populate a custom attachment metadata field with data from the image’s EXIF data?

You can hook into the add_attachment and edit_attachment action hooks and use PHP’s exif_read_data() to add the EXIF data to your attachment’s metadata:

<?php
add_action( 'add_attachment', 'wpse302908_add_exif' );
add_action( 'edit_attachment', 'wpse302908_add_exif' );

/**
 * Inserts metadata from an image's EXIF data.
 *
 * @param  int $post_id The attachment's post ID.
 * @return void
 */
function wpse302908_add_exif( $post_id ) {
    // These are the keys for the data you want from the EXIF data.
    $exif_keys = array( 'key_1', 'key_2' );
    $exif_data = exif_read_data( get_attachment_image_url( $attachment_id );
    foreach ( $exif_keys as $exif_key ) {
        update_post_meta( $post_id, $exif_key, $exif_data[ $exif_key ] );
    }

}

Notes

  • This code is untested.
  • I assumed that your desired meta key names match the ones provided in the image’s EXIF data.
  • This code doesn’t check to make sure you’re uploading an image; you’ll probably want to insert some checks for that.

References