function set_image_alt_text_from_title($attachment_ID) {
$attachment = get_post($attachment_ID);
// Check if the attachment is an image
if ($attachment && strpos($attachment->post_mime_type, 'image') !== false) {
// Get the image's title
$image_title = $attachment->post_title;
$alt_text = str_replace("-"," ",$image_title);
// Update the image's alt text with the title
// Update the image's title with the alt text
if ($alt_text && !empty($alt_text)) {
wp_update_post(array(
'ID' => $attachment_ID,
'post_title' => $alt_text
));
}
update_post_meta($attachment_ID, '_wp_attachment_image_alt', $alt_text);
}
}
add_action('add_attachment', 'set_image_alt_text_from_title');