By default, WordPress saves the image file name as the title and leaves the alt, caption and description blank. The following script saves the file name into the image meta fields during file upload. Add the script to the functions.php of the child theme to change that behaviour. There are two pieces, the original one and a modified one. Add this to functions.php
- Title
- Alt
- Caption
- Description
Original version
Source: https://wpkraken.io/blog/wordpress-images
// Set image meta during upload
// Source: https://wpkraken.io/blog/wordpress-images/
add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
$my_image_title = get_post( $post_ID )->post_title;
// Sanitize the title: remove hyphens, underscores & extra spaces:
$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',$my_image_title );
$my_image_title_no_size = preg_replace( '([0-9]*x[0-9]*)', '',$my_image_title );
// Sanitize the title: capitalize first letter of every word other letters lower case):
//* remove this comment if needed *// $my_image_title = ucwords( strtolower( $my_image_title ) );
// Create an array with the image meta (Title, Caption, Description) to be updated
// Note: comment out the Excerpt/Caption or Content/Description lines if not needed
$my_image_meta = array(
// Specify the image (ID) to be updated
'ID' => $post_ID,
// Set image Title to sanitized title
'post_title' => $my_image_title_no_size,
// Set image Caption (Excerpt) to sanitized title
//* remove this comment if needed *// 'post_excerpt' => $my_image_title,
// Set image Description (Content) to sanitized title
'post_content' => $my_image_title,
);
// Set the image Alt-Text
update_post_meta( $post_ID, '_wp_attachment_image_alt',$my_image_title_no_size);
// Set the image meta (e.g. Title, Excerpt, Content)
wp_update_post( $my_image_meta );
}
}
add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
$my_image_title = get_post( $post_ID )->post_title;
// Sanitize the title: remove hyphens, underscores & extra
// spaces:
$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',
$my_image_title );
// Sanitize the title: capitalize first letter of every word
// (other letters lower case):
$my_image_title = ucwords( strtolower( $my_image_title ) );
// Create an array with the image meta (Title, Caption,
// Description) to be updated
// Note: comment out the Excerpt/Caption or Content/Description
// lines if not needed
$my_image_meta = array(
// Specify the image (ID) to be updated
'ID' => $post_ID,
// Set image Title to sanitized title
'post_title' => $my_image_title,
// Set image Caption (Excerpt) to sanitized title
'post_excerpt' => $my_image_title,
// Set image Description (Content) to sanitized title
'post_content' => $my_image_title,
);
// Set the image Alt-Text
update_post_meta( $post_ID, '_wp_attachment_image_alt',
$my_image_title );
// Set the image meta (e.g. Title, Excerpt, Content)
wp_update_post( $my_image_meta );
}
}Code language: PHP (php)
Modified version
This one does not
- Capitalize the first character of each word
- Set the image caption
- Removes the image size from the title and alt (not description) if it is in the file name like 1080×1080
// Set image meta during upload
// Modified from source: https://wpkraken.io/blog/wordpress-images/
add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
$my_image_title = get_post( $post_ID )->post_title;
// Sanitize the title: remove hyphens, underscores & extra spaces:
$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',$my_image_title );
$my_image_title_no_size = preg_replace( '([0-9]*x[0-9]*)', '',$my_image_title );
// Sanitize the title: capitalize first letter of every word other letters lower case):
//* remove this comment if needed *// $my_image_title = ucwords( strtolower( $my_image_title ) );
// Create an array with the image meta (Title, Caption, Description) to be updated
// Note: comment out the Excerpt/Caption or Content/Description lines if not needed
$my_image_meta = array(
// Specify the image (ID) to be updated
'ID' => $post_ID,
// Set image Title to sanitized title
'post_title' => $my_image_title_no_size,
// Set image Caption (Excerpt) to sanitized title
//* remove this comment if needed *// 'post_excerpt' => $my_image_title,
// Set image Description (Content) to sanitized title
'post_content' => $my_image_title,
);
// Set the image Alt-Text
update_post_meta( $post_ID, '_wp_attachment_image_alt',$my_image_title_no_size);
// Set the image meta (e.g. Title, Excerpt, Content)
wp_update_post( $my_image_meta );
}
}Code language: PHP (php)