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

Automatically Add Description, Caption And Alt Text From Image Title

Do you want to add automatically image Description, Caption, Alt text and image title for your uploaded image details in your WordPress post? Because this is best for Search Engine Optimization (SEO), When you upload a new media file to WordPress it creates a new Attachment. An Attachment is in effect a type of post, and as such an Attachment has a Title, an Excerpt and Content just like regular posts do.

In the case of an Attachment, the Excerpt is used to store the image Caption, and the Content is used to store the image Description. For more example see the below image figure.

Illustration For Automatic Add All Text Titile And Description From Image Title
Illustration For Automatic Add All Text Titile And Description From Image Title

INSTRUCTION

Copy the below whole code and paste into your functions.php file direct from your installed themes folder.

function image_details( $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_content’] = $attachment_title; // Image Description
$uploaded_image[‘post_excerpt’] = $attachment_title; // Image Captionwp_update_post( $uploaded_image );
update_post_meta( $post_ID, ‘_wp_attachment_image_alt’, $attachment_title ); // Image Alt Text
}
add_action( ‘add_attachment’, ‘image_details’ );
  1. Login to your WordPress Dashboard
  2. Go to “Appearance” menu from left sidebar
  3. Click on “Theme Editor” options from appearance menu.
  4. Now find the functions.php file from right sidebar.
  5. Now paste the above code inside functions.php file
  6. Finally click on “Update File” and save your work
Add Image Caption Description Title And Alt Text On Your WordPress Post Image
Add Image Caption Description Title And Alt Text On Your WordPress Post Image

I hope you enjoyed.

In the end

I hope you have found this article helpful. Let us me your opinion or questions if any through the comment form in below or use this form to ask your question.

You May Also Like

About the Author: Kedar Adhikari

I am WordPress Themes developer and designer and also any type of website design with minimum cost. for more information you can contact me on email [email protected]

Leave a Reply

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

Copyright © 2024 My WP Helper. All rights reserved.