WordPress 2.9 has several new image enhancements. One of the biggest features is some basic image editing functionality. Another one is that you can now specify different alt text from the “caption” field. The “caption” field places a caption under the image. The “alt” text is used to describe the picture to non-seeing users (including search engines). This is a nice addition. However, I usually like my caption, alt text, and title text to be all the same, and I don’t like to have to enter it all manually or copy and paste. By default, wordpress will use an IPTC caption as its “description” field, which shows up in the title attribute of the image. This is nice, since I can add a caption in my image editing program of choice (Picasa) and then I don’t have to enter it again. Except for those pesky alt and caption fields, which are blank by default.

This is particularly important if I am uploading many pictures. So I wrote a little sql which will set the “caption” and “alt text” fields to be the same as the description. I already had this working for the “caption” field for quite some time, but getting it to work for the different alt text handling in 2.9 was a bit tricky, since I discovered that the alt text is not stored in wp_posts like the other fields. Instead it is stored in the wp_postmeta table, which is new in 2.9. Although it took me awhile to figure this out, the new table is a welcome addition. Now for each image you upload, wordpress stores meta information in this table, including the width and height, different sized versions of the file, the IPTC caption, some EXIF info and a few other goodies. This means that if you like to include EXIF info about your pictures, that doing so requires only a simple database lookup, instead of having to read the headers from the image, which should be considerably faster I would imagine.

Now for my little SQL which sets the “caption” and “alt text” to be the same as the description. I run this on my server after uploading pictures. It would be even better if I could figure out how to do this as a wordpress plugin.

-- First we set the image caption (post_excerpt) to be the same as the
-- description (post_content)
update wp_posts set post_excerpt=post_content WHERE post_type='attachment' AND
date(post_date)=curdate();

-- Next we set the alternative text to be the same as the post_excerpt
insert into wp_postmeta (wp_postmeta.post_id, wp_postmeta.meta_value) select
distinct wp_posts.ID, wp_posts.post_excerpt from wp_posts, wp_postmeta where
wp_posts.ID=wp_postmeta.post_id and post_type='attachment' AND
date(post_date)=curdate();

-- This line sets the correct meta_key for the previous line, which doesn't
-- seem possible otherwise
update wp_postmeta set meta_key='_wp_attachment_image_alt' where meta_key IS
NULL;