How to add “alt” attribute for image during upload at WP front-end?

The alt text is stored in the postmeta database table by WordPress, therefore we have to update the postmeta of the given attachment. The reference is done via the unique attachment ID, and the meta_key we are targeting is the _wp_attachment_image_alt.

So, to set the alt text, we can do the following:

update_post_meta($attach_id, '_wp_attachment_image_alt', 'Your alt text');

https://developer.wordpress.org/reference/functions/update_post_meta/