function display_exif_fields ( $form_fields, $post ){
$type = get_post_mime_type( $post->ID );
$attachment_path = get_attached_file( $post->ID );
$metadata = wp_read_image_metadata_exif( $attachment_path );
$city = get_post_meta( $post->ID, 'city', true );
$locationname = get_post_meta( $post->ID, 'locationname', true );
$form_fields['city'] = array(
'label' => 'City',
'input' => 'text',
'value' => !empty($city) ? $city : $metadata['city'],
'helps' => '',
);
$form_fields['locationname'] = array(
'label' => 'Location name',
'input' => 'text',
'value' => !empty($locationname) ? $locationname : $metadata['locationname'],
'helps' => '',
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'display_exif_fields', 10, 2 );
function save_exif_fields( $post, $attachment ) {
$array = [ 'city', 'locationname' ];
foreach ( $array as $one ) {
if ( ! empty( $attachment[ $one ] ) ) {
update_post_meta( $post[ 'ID' ], $one, $attachment[ $one ] );
} else {
delete_post_meta( $post[ 'ID' ], $one );
}
}
return $post;
}
add_filter( 'attachment_fields_to_save', 'save_exif_fields', 10, 2 );