Automatically Add Image Caption, Description And Alt Text From Image Title

I do not use that many images in my posts, but when I do, I do it right.

By that I mean, I make sure the image is cropped and optimized for the web (I use Kraken.io) and I rename the image to mean something rather than it saying IMG_0507.img. While naming stuff, I like to capitalize the first letter of each word. It all looks so good.

Until I upload it to WordPress. And then WordPress does this crap. ‘Neatly Titled Image’ becomes..

Neatly Titled Image - No More

Neatly Titled Image – No More

For years I have ignored this and updated it manually, but today it is time to fix it, once and for all. And in the battle for cleaner (and easier) image attributes, I hope I am not alone.

How WordPress Stores Image Attributes

WordPress stores media attachments in the wp_posts table. Which is good news because, then we can simply use the wp_update_post() function to easily update it’s contents.

Attachment Details Stored In Wp_posts Table

Attachment Details Stored In wp_posts Table

At the same time, Image Alt is saved in wp_posts_meta table with the meta_key: _wp_attachment_image_alt

Image Alt Saved In Wp_posts_meta Table

Image Alt Saved In wp_posts_meta Table

The wp_posts_meta table can be altered using the update_post_meta() function.

Auto Neatified Image Attributes From Title

While inserting an image, I like to copy my neat image title and use it for Image Alt Text, Image Description and in most cases as the Image Caption.

To do all this automatically, we can hook onto to the add_attachment action of WordPress. Here is how its done.

// Auto Add Image Attributes From Image Filename
function abl_mc_auto_image_attributes( $post_ID ) {
$attachment = get_post( $post_ID );
$attachment_title = $attachment->post_title;
$attachment_title = str_replace( '-', ' ', $attachment_title ); // Hyphen Removal
$attachment_title = ucwords( $attachment_title ); // Capitalize First Word
$uploaded_image = array();
$uploaded_image['ID'] = $post_ID;
$uploaded_image['post_title'] = $attachment_title; // Image Title
$uploaded_image['post_excerpt'] = $attachment_title; // Image Caption
$uploaded_image['post_content'] = $attachment_title; // Image Description
update_post_meta( $post_ID, '_wp_attachment_image_alt', $attachment_title ); // Image Alt Text
wp_update_post( $uploaded_image );
}
add_action( 'add_attachment', 'abl_mc_auto_image_attributes' );

Copy the snippet into your functions.php and the next time you upload an image, here is how it will look.

Neatly Titled Image Finally

Neatly Titled Image – Finally

I can’t believe I didn’t do this earlier.

Want To Update Your Existing Images In Media Library As Well?

I have written a WordPress plugin that can bulk update image attributes for your existing images. This plugin can update BOTH your new uploads and existing uploads in the media library.

Find it here: Auto Image Attributes From Filename With Bulk Updater.

Hello, I am Arun Basil Lal. Thank you for reading!

I am a freelance WordPress developer with over a decade of experience. I am passionate about making things and traveling the world.

Divi WordPress Theme - My Review

Divi WordPress Theme
Divi is a WordPress theme that web designers do not want you to know. It comes with a drag-and-drop theme builder. You can build beautiful looking unique websites without touching a line of code. Just choose from one of the many pre-made layouts, or pick elements and arrange them any way you like.

Divi is every WordPress developer's wet dream. Surprise your clients with neat responsive websites and have fun building them.

Divi comes from Elegant Themes. If you enjoy building websites, you *need* an Elegant Themes membership. 87 beautiful themes and 5 plugins for the cost of less than a candy-bar each!


Note: I am an avid user of Divi myself and this is a honest review. I wouldn't recommend something that I do not personally find amazing.

7 Comments.

  1. Payal sharma says:

    Great Work Arun.
    It is very informative blog with clear description thank you for sharing the blog, well done.

  2. sunny says:

    can we do that same to old image of wordpress

    this is only work to new image
    give any idea so that i can do same on old pic as well

  3. John Fridinger says:

    Hi Arun,

    I left you a response in your new plugin”s support forum… It works beautifully, for dashes, but not for under_scores… You’ll see what I wrote… And, I am so very grateful that you were called to write this plugin, and hugely hoping you will add underscores to what it replaces with a space….

    Kind regards,
    John.

1 Pings / Trackbacks.

  1. […] Few months back I had written a tutorial on how to automatically set image Title, Caption, Description and Alt Text from the filename of the image. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Share With Friends

Email Stamp

Free Newsletter

Get my newest blog posts and insider tips. No more than a few emails a year.

Just enter your email address below and click Sign Up Now

Please enter all required fields
Correct invalid entries

You have Successfully Subscribed!