Programmatically add featured image with ALT text in WordPress?

There are examples of how to add post with featured image programmatically in wordpress, but I need to add post with featured image and ALT text too. How can I do something like that?

Thank you.

Related posts

Leave a Reply

1 comment

  1. If you look at the source for the wp_get_attachment_image function, you can see how the alt text is being fetched:

    'alt'   => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ))
    

    From there it is easy enough to figure out how to update the alt text.

    update_post_meta($attachment_id, '_wp_attachment_image_alt', 'My Alt Text');