EXIF Revisited

Note: Some things changed in WordPress 3.5, which broke some of what I did here. I’ve since refined this a bit, and changed a few other things. There should be an updated report at some point…
Warning: This post contains references to PHP, EXIF and possibly other acronyms. It also contains references to coding that may confuse the non-technical and make actual programmers point and laugh.

Some time last year, I got quite excited when I managed to find a code example that fixed a slight WordPress issue I’d been having. This involved automagically copying the title of uploaded images (as set in Lightroom) into the Caption field of the WordPress image uploader. This saved me a second or two every time I uploaded an image, not to mention saving me calling myself rude names when I forgot to to the copying and had to go back and edit the post again. All was well with this until the recent release of WordPress 3.4.

In an effort to make things more useful, the WordPress developers have made it possible for image captions to contain actual HTML rather than just plain text – this means you can put in links to credit people, your choice of formatting, or indeed pretty much anything you like in a caption. Nice. Unfortunately, this broke my bit of code, which incidentally, I moved from the theme’s functions.php into my own “functionality plugin” – basically a small file that contains useful bits of code that I want to be available even when I change to a new theme.

I scratched my head for a bit, removed the splinters, then found the reason. It just shows how rusty my HTML is – I should have realised that the only way to make the new caption thingy work was to change it from being a standard “text” field into a textarea, and that unlike a text box, a textarea doesn’t have a “value” property to drop a value into, so this wouldn’t work:

$form_fields['post_excerpt']['value'] = $post->post_title;

Well, I did a bit of searching, and eventually found a post by Otto describing a problem he had run into when inserting images from Picasa. He included the code of a plugin he’d written to fix this, and I realised that with minimal modification, it would do what I wanted.

I then did a bit more thinking about they way I use Thesography to insert EXIF data. Since WordPress 3.4 was released, I’d been adding the [exif] shortcode to the caption rather than putting it in below the image, as this seemed a bit tidier. To make that work, as the caption is itself an automatically generated shortcode, I had to use the useful Outerbridge Nested Shortcodes Plugin – no work required, just install and activate in the usual way.

As I was going to be using a bit of code to insert a value into the Caption field, why not insert the [exif] code, too? So, I adapted the bit of code a bit further, and managed to get it to insert the id of the image, which removed the one manual task I’d left myself. I then did a bit more tweaking of the Thesography plugin to get things formatted the way I wanted, and that was that.

For anyone who’s interested, this is what I’m doing:


add_filter( 'wp_read_image_metadata', 'set_caption_from_title' );
function set_caption_from_title($meta) {
 if (empty($meta['caption']) && !empty($meta['title'])) {
 $meta['caption'] = $meta['title'];

 }
 return $meta;
}

add_action( 'add_attachment', 'set_wp_caption_from_description' );
function set_wp_caption_from_description($id) {
 $attachment = & get_post( $id, ARRAY_A );
 if ( !empty( $attachment ) ) {
 $attachment['post_excerpt'] = '[exif id="' . $id .'" show="caption,camera,aperture,iso,shutter,focus,location,time"]';
 $attachment['post_content'] = '';
 wp_update_post($attachment);
 }
}

add_filter('attachment_fields_to_edit', 'image_caption_to_title', 11, 2 );

function image_caption_to_title( $form_fields, $post ) {
 if ( substr( $post->post_mime_type, 0, 5 ) == 'image' ) {
 $form_fields['image_alt']['value'] = $post->post_title;
 }
 return $form_fields;
 }

All of which means that I spend less time inserting each image into a post, which means I can do lots more of them.

2 thoughts on “EXIF Revisited

  1. Pingback: Weight and Stuff Report – 17 June 2012 : Losing it

  2. Pingback: EXIF Again | Losing it

Comments are closed.