Daily Archives: Sunday, 17th Jun 2012

Weight and Stuff Report – 17 June 2012

And another small rise today, which I’m sure is not even slightly related to that large portion of chilli I had last night.

It’s been another quiet day in – better weather than yesterday, so going out would have been an option, but as I didn’t get round to doing the washing yesterday, I had to get it done today, so that’s what I did.

Here’s some useful information:

Good advice

Good advice

Camera: Canon EOS 5D Mark III
Aperture: ƒ/2.8
Shutter speed: 1/800s
Focal length: 50mm
ISO: 100
Taken: 13 June, 2012
Location: 54° 58.3295′ 0″ N 1° 36.4989′ 0″ W

One thing I didn’t mention in my EXIF mutterings earlier was that I was hoping to get the lens type listed with the other photographic details. After much searching and reading, I learned that this is stored in an arcane format that is a bit tricky[1] to extract. It’s certainly beyond any PHPery[2] that I could aspire to, so you’ll have to live without it.

[1] Understatement
[2] It’s a word :yes:

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'] = '';

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.