19 05 2011
Automatically add alt tags to wordpress featured images
I was working on a site recently that had a few thousand images imported to wordpress after a drupal conversion, these images were set as the featured image for the imported posts. Because of the way the old site was constructed, these images had no meaningful names.
You can use the mysql query below to set the alt tags of your featured images to the name of the post.
This query assumes that no alt tag data currently exists, if you have existing alt tags, you will need to use an update query instead. Also note this only sets the alt data for images set as featured images for posts.
1 2 3 4 5 6 |
INSERT INTO wp_postmeta (post_id, meta_key, meta_value) SELECT pm.meta_value image_post_id, '_wp_attachment_image_alt', p.post_title FROM wp_postmeta pm LEFT JOIN wp_posts p ON p.id = pm.post_id WHERE pm.meta_key = '_thumbnail_id' AND pm.meta_value IS NOT NULL |
Fixes for vbulletin forums hacked by Team Animus Non-clickable placeholder in WordPress Navigation Menu
Hi Greg. Thanks for writing this useful code. I was just wondering where exactly to insert it?
Hi Sally, this is mysql query, so you can run it things such as phpmyadmin.
Will this work for pages too? My current theme doesn’t seem to display the alt text or title even when those fields have data in the media library.
How do you do the same for Featured Image titles? instead of alt tags.
I mean “Automatically add titles to wordpress featured images”. Amy help would be appreciated.
Thank You.