Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dotherightthing/897733b728be9bf40f67bac3b77b7702 to your computer and use it in GitHub Desktop.
Save dotherightthing/897733b728be9bf40f67bac3b77b7702 to your computer and use it in GitHub Desktop.
[Storing Geolocation EXIF with the attachment post] #wordpress

Storing Geolocation EXIF with the attachment post

Created: 2017.04.09

This code causes the Geolocation EXIF to the attachment metadata stored in the database.

It is run when an image is uploaded into WordPress.

I found that by adding a call to 6q3rVgtQsiH0820S_2_read_image_metadata into my dtrt_gallery_thumbnail_link function (hooked into 6q3rVgtQsiH0820S_2_get_attachment_link) resulted in the attachment metadata being added for existing images.

/**
* Add Geolocation EXIF to the attachment metadata stored in the WP database
* Source: http://kristarella.blog/2009/04/add-image-exif-metadata-to-wordpress/
*/

include_once( ABSPATH . 'wp-admin/includes/image.php' ); // for access to wp_read_image_metadata

add_filter('wp_read_image_metadata', 'add_geo_exif','',3);

function add_geo_exif($meta,$file,$sourceImageType) {

$exif = @exif_read_data( $file );

if (!empty($exif['GPSLatitude'])) {
$meta['latitude'] = $exif['GPSLatitude'] ;
}
if (!empty($exif['GPSLatitudeRef'])) {
$meta['latitude_ref'] = trim( $exif['GPSLatitudeRef'] );
}
if (!empty($exif['GPSLongitude'])) {
$meta['longitude'] = $exif['GPSLongitude'] ;
}
if (!empty($exif['GPSLongitudeRef'])) {
$meta['longitude_ref'] = trim( $exif['GPSLongitudeRef'] );
}

return $meta;
}

Reprocessing everything

Ideally I would like to create a plugin which loops over all the images and adds this metadata. This would be similar to the Regenerate Thumbnails plugin, except that nothing is being generated, and only the full size images are being processed.

But even so there would likely be a delay while the extraction took place, so it would need a similar interface where progress was tracked via Ajax calls etc.

I think the lady who wrote this code has created a plugin already, but I think it does much more than I need it to do.

Reprocessing per day

I could also process images per day, but the Admin Columns plugin doesn't support ACF fields except via a USD 100/year upgrade, so it's not easy to sort images by the 'parent' day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment