Setting alt-text when attaching image
-
Hi!
I’m trying to insert an image attachment into the media gallery and insert it into the open post programmatically. I’m sort of half way there. The image is inserted into the gallery with an alt-text and inserted into the post but in the post it is missing the alt text.
This is how I’m adding the image:
$attachment_id = wp_insert_attachment( $attachment, $image_file_path, $post_id ); update_post_meta( $attachment_id, '_wp_attachment_image_alt', $my_alt_text);
I’m guessing the problem is that I’m changing the alt text after the image is inserted into the post and that I somehow should pass the alt text to wp_insert_attachment() instead. But I can’t figure out how. Neither of these methods work:
$attachment["alt"] = "..."; $attachment["_wp_attachment_image_alt"] = "..."; $attachment_id = wp_insert_attachment( $attachment, $image_file_path, $post_id );
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Setting alt-text when attaching image’ is closed to new replies.