Adding EXIF fields to wp_read_image_metadata (6 posts)

  1. kristarella
    Posted 7 years ago #


    I've been at this on and off for months and have searched extensively without finding the answer.

    I would like to filter the function wp_read_image_metadata (in wp-admin/includes/image.php) to add latitude and longitude values to the image meta in the database. I have been able to achieve this in the past by editing the file directly (as per http://www.kristarella.com/2008/12/geo-exif-data-in-wordpress/). However I would like to put it in a plugin or theme functions file to avoid having to edit the file every WP update.

    So far I have:

    function add_geo_exif($meta,$file,$sourceImageType) {
    	list(,,$sourceImageType) = getimagesize( $file );
    		$exif = exif_read_data( $file );
    			$meta['latitude'] = $exif['GPSLatitude'] ;
    			$meta['latitude_ref'] = trim( $exif['GPSLatitudeRef'] );
    			$meta['longitude'] = $exif['GPSLongitude'] ;
    			$meta['longitude_ref'] = trim( $exif['GPSLongitudeRef'] );
    	return $meta;
    add_filter('wp_read_image_metadata', 'add_geo_exif');

    That code successfully filters the function and adds those fields to the database, but the values are empty. I think it's because my function doesn't know what $file is, but I can't figure out what I need to add to define $file.

    Appreciate any insight anyone can provide into this. Thanks!

  2. ruhanirabin
    Posted 7 years ago #

    1. if u want to manipulate the exif_data u need to pass a image filename to the variable $file

    2. the exif_read_data function said that you can't use a URL of a image to grab the information so it would be something like physical file location

  3. kristarella
    Posted 7 years ago #

    Thankyou. That explains why my fleeting attempt with the metadata url didn't work.

    Do you know how this applies to the context of image.php? I have been tracking this variable across several files and just don't know how it applies to the current image being uploaded.

    Your point #2 may explain the part of wp-admin/includes/image.php which says

    // Make the file path relative to the upload dir
    		if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
    			if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
    				$file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
    				$file = ltrim($file, '/');

    However $file is still coming through as a function parameter into that function.

  4. ruhanirabin
    Posted 7 years ago #

    its most probably the data is not passed to the filter.. i'm not sure about image.php .. if you have a working hack of image.php , it might help others to understand what you want to achieve..

  5. benbarden
    Posted 7 years ago #

    You need to call add_filter with more parameters. By default your filter will only have 1 parameter. To use three, change the last line of your code to the following:

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

    More info here (see "Hook in your filter"):


  6. kristarella
    Posted 7 years ago #

    Thank you both for your time and help!

    It wasn't grabbing $file because there was only 1 parameter being passed! Nice one Ben!

Topic Closed

This topic has been closed to new replies.

About this Topic