How can I require users to enter alt text when adding attachments?
Alt text is surprisingly buried; the only way I've been able to retrieve the alt text at all is via the wp_get_attachment_image()
function, thusly:
add_filter('wp_insert_attachment_data', 'print_metadata', 10, 2);
function print_metadata( $data, $uncleaned_data ) {
$attachment_id = $arr['ID'];
$meta = wp_get_attachment_image($attachment_id);
error_log($meta);
}
Which produces just some crappy HTML:
<img width="150" height="150" src="http://domain.local/wp-content/uploads/2016/05/cat2-1-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="My alt text" />
So, I might parse this HTML, after which I might try and throw some sort of validation error. This seems quite hacky, and may also be bad UX (since it would not decorate the Alt Text field itself with a "required" asterisk or anything).
I really wish I could just use the filter above, wp_insert_attachment_data
, and just reject any array without alt text; alas, alt text is not in either $data
or $uncleaned_data
. Or apparently anywhere else for that matter.
Any other ideas? Here's a picture of the interface: