set_image_alt_text_from_title

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');

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest
Author picture

SitiWeb is de expert op het gebied van WordPress. gespecialiseerd in het leveren van maatwerk.

Leave a comment

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *