I would like randomly assign an image an alt text based on taxonomy entered by the user on a post that the image is attached if there is no alt text entered.

For example, on a post that has an image, I would like to randomly assign something like: "Used Camping Trailer in North Carolina." Where the USED and CAMPING TRAILER would be pulled from data entered on the post.

I have this working on page load, but obviously, this would be bad SEO because it's going to change on every page load. Therefore is there a way to do it and it be entered into the database.

1 Answer 1


Just set the image alt text when the image is saved. Hook onto the save_post action, check if any of the attached images don't have alt text, and if not then generate the alt text you want and save it with:

update_post_meta( $attachment_id, '_wp_attachment_image_alt', 'this is the alt text' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.