• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Home
  • About
  • My Blogs
    • Photoblog
    • Knots
    • WP.com tips
kristarella.com

kristarella.com

Happiness Engineer at Automattic, lover of knitting, crochet, sci-fi and more

  • Presentations
  • Plugins
    • Exifography
  • Contact
You are here: Home / Technology / Add image EXIF metadata to WordPress

Add image EXIF metadata to WordPress

3 April 2009 by kristarella

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.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on Pocket (Opens in new window) Pocket

Like this:

Like Loading...

Related

Filed Under: Technology, Tutorials, WordPress Tagged With: geek, PHP

Reader Interactions

Comments

  1. Khürt Williams says

    1 May 2009 at 12:24

    Thanks. This is extremely useful. Now off to go try it.

  2. Khürt Williams says

    2 May 2009 at 14:43

    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?

  3. kristarella says

    2 May 2009 at 14:50

    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. 😉

  4. Khürt Williams says

    2 May 2009 at 15:03

    If only I would RTFB (read the fracking blog) I would have caught that. My fault. Thanks for responding and doing what you do.

  5. kristarella says

    2 May 2009 at 15:05

    No worries, I fail at skim reading all the time and leave questions that were answered in the post 😛

  6. Rainer Schleevoigt says

    4 January 2010 at 23:20

    Hi,
    I’am looking for a possibility to write geoinfos into an image. There is a Perl-Lib http://www.sno.phy.queensu.ca/~phil/exiftool/ , but I want to use php nativ..

    Rainer

  7. kristarella says

    5 January 2010 at 10:44

    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.

A triptych of baubles. These are very satisfying t A triptych of baubles. These are very satisfying to paint ❤️💚💙

#watercolor #watercolour #christmas
I have really enjoyed this Christmassy painting! S I have really enjoyed this Christmassy painting! So glad my friend asked me to make some cards for our church helpers, because it set me on a roll!
#watercolor #watercolour #christmas #christmascards #watercolorcards
Had a great Saturday at the GKR Karate State Title Had a great Saturday at the GKR Karate State Titles! I was a sub for @jamesxuereb.me in the Blue Flame Dragons thanks to his sprained ankle; we won first round and came fourth overall!
I did a bunch of judging and officiating, which was really good.
I didn’t place in my individual events, but had a very fun final round of kumite (sparring).

#gkrkarate #karate
More stamping tonight. Even better than last night More stamping tonight. Even better than last night’s!
Did some stamping this evening. Love it! I wish I’d done some pages in other ink colours before I dismantled the stamps 😂
Had an appointment in the city, so I got to visit Had an appointment in the city, so I got to visit the @legocertifiedstores_anz Wicked display!
#wicked #lego #afol #sydney
A little book I made from Bunnings paint sample ca A little book I made from Bunnings paint sample cards. It’s going to be for mini paintings and collages. Sometimes it’s nice to start with a colour rather than a blank white page!
A little while ago I did some swatching of Daniel A little while ago I did some swatching of Daniel Smith and Schminke Horodam watercolours. So soothing! I love some of the granulating colours!
#watercolours
Follow on Instagram

Footer

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Top Posts & Pages

  • Home
    Home
  • Wooden yarn swift
    Wooden yarn swift
  • Edit EPS files in Inkscape on Mac
    Edit EPS files in Inkscape on Mac

Follow Me On…

  • Instagram
  • X
  • GitHub
  • LinkedIn
  • YouTube

Categories

Copyright © 2025 · Kristen Symonds · kristarella.com

%d