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.
Copy the below whole code and paste into your functions.php file direct from your installed themes folder.
$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’ );
- Login to your WordPress Dashboard
- Go to “Appearance” menu from left sidebar
- Click on “Theme Editor” options from appearance menu.
- Now find the functions.php file from right sidebar.
- Now paste the above code inside functions.php file
- Finally click on “Update File” and save your work
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.