Another little annoyance fixed

As I’m adding a lot more images to the site these days, I’ve been getting a wee bit annoyed with a minor problem. When I upload images that I’ve processed in Lightroom, WordPress helpfully uses the Lightroom “Title” value for the image, but if I want to use that as the caption or alt text, I have to copy and paste it. Not a big deal, but it slows me down a bit.

I did some searching, looking for plugins, Lightroom settings, and indeed actual code, but it took a while. Then, on my third or fourth attempt at a search string, I found the answer on the WordPress support forums. All I needed to do was add some code to my theme’s functions.php file:

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['post_excerpt']['value'] = $post->post_title;
$form_fields['image_alt']['value'] = $post->post_title;
}
return $form_fields;
}

And now when I upload an image, the alt text and caption fields are filled in for me.

Original solution by Mark / t31os in this thread.  I added an extra line because I wanted to have the alt value filled in too.

Being able to add little bits of code like this to a file in a child theme is one of the best features of WordPress for me. It’s quicker and easier than creating a plugin, which generally requires some additional bits and bobs[1] before it’ll work.

[1] This is not a technical term. I am not, and never will be a coder – my brain doesn’t seem to work the right way for that…

One thought on “Another little annoyance fixed

  1. Pingback: EXIF Revisited : Losing it

Comments are closed.