PRoman
PRoman

Reputation: 13

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.

Upvotes: 1

Views: 4308

Answers (1)

Mathew Tinsley
Mathew Tinsley

Reputation: 6976

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

Upvotes: 6

Related Questions