Add image EXIF metadata to WordPress

In December I wrote about adding EXIF fields for geo locations to the image features already in WordPress. The first part of that post involved making changes to the WordPress file controlling image handling. They were simple changes, but annoying to have to change with every WP upgrade.

After several months I have finally figured out — with excellent help from Ben Barden — how to filter the read_metadata function and add EXIF fields to the WordPress database without having to edit core WordPress files.

The following function can be added to the functions.php file of your theme (or custom_functions.php if you use Thesis).

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;
}
add_filter('wp_read_image_metadata', 'add_geo_exif','',3);

The main features of the filter to note are:

  • My function, add_geo_exif has 3 parameters that I don’t define, $meta, $file and $sourceImageType.
    I probably don’t need the last one because I don’t refer to it in the function, but it does no harm to leave it there. The first one is an array in the WordPress function wp_read_image_metadata, which this function adds values to. And $file is the file that is being uploaded by WordPress, defined elsewhere in the WP core files.
  • The 3 at the end of add_filter means that this filter allows three parameters to be passed.
    That’s part of what took me so long to get this working, I hadn’t given that argument and the default is 1, so my function wasn’t getting $file.
  • The if(!empty lines tell the function to only filter the EXIF data in if it exists in the image, so you shouldn’t get alot of extraneous stuff in the database.

After running this filter you can access the information as outlined in the geo EXIF post, in the section calling metadata from the database.

Comments

  1. says

    I’m stuck. I don’t have any idea how to make this work for me. I understand the PHP code .. just don’t know where to use it with Thesis. Do you have some time to help?

  2. says

    Hi Khürt, I was going to mention it before, but I thought I’d wait to see if you needed it said, since it says it at the end of the post. This function is only to get WordPress to import the geo exif into the database. It already imports shutter speed, aperture etc. To use the data in your theme you need some other functions in my previous post about Geo EXIF.
    You will probably want to look at the code from Blogging Tips: WordPress gallery and EXIF as well as Mixing Gallery and blog posts.

    No you see why I was considering making it a plugin. ;-)

  3. says

    Rainer — I have no idea how to do that. I’ve only investigated software options for writing location data to images, anything I’ve done with PHP is only reading the EXIF from the image, I don’t know if it’s possible to write EXIF to an image with PHP.