We can upload images to wordpress using php code. However, how to add alt text for these images? In this tutorial, we will introduce you how to do.
Image is a type of post in wordpress
All images uploaded are saved in wp_posts table in wordpress, which means image is regarded as a type of wordpress post. We can use wordpress post functions to operate images.
Where image alt text is saved?
Image alt text is a meta data of an image post, it is saved in wp_postmeta table. Here is an example:
How to add alt text for images in wordpress?
As mentioned above, we can add or update image post meta data ‘_wp_attachment_image_alt‘.
Here is an example:
update_post_meta($attach_id, '_wp_attachment_image_alt', $alt);
$attach_id: it is the image post id.
$alt: the content of image alt text.
If _wp_attachment_image_alt meta data does not exist, it will be created. Otherwise, it will be updated.