5

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:

WP interface

11
  • 1
    If you can't modify the form directly, I'd probably consider using jQuery for this. Enqueue your script in the admin area only, select the field and decorate it as such. You could then either set its required attribute to true, or hook into the saving function somehow to check that it's done.
    – Tim Malone
    May 4, 2016 at 21:25
  • An interesting idea; I'm not sure how it would work, though. Adding a "required" property to the input tag would not get triggered, because the fields are sent via ajax on loss of focus, not on submit (because there's no submit button).
    – allanberry
    May 4, 2016 at 22:00
  • Got it. You'd probably need to hook into the close of the dialog then, to stop it from closing if the field is empty.
    – Tim Malone
    May 4, 2016 at 22:02
  • Yes. But you'd also have to hook into the left/right navigation buttons (both for the browser and within the dialog), the closing of the window altogether, and every link therein. In addition, this functionality happens in multiple dialogs, on several different pages. It all seems very messy.
    – allanberry
    May 4, 2016 at 22:04
  • @TimMalone Stopping the user from closing the dialog without Alt Text seems extreme. I think the easiest and most user-friendly would be to just not let them insert the image into a post if it doesn't have any alt text. This interests me :)
    – Howdy_McGee
    May 4, 2016 at 22:14

1 Answer 1

2

The code below will only run once whenever a file is uploaded.

  1. We keep an array ( $image_mimes ) of acceptable image-mimetypes
  2. We get the current attachment mime type
  3. We make sure what is given is indeed an image ( because we don't need unnecessary postmeta cluttering our table )
  4. We grab the title from the attachment and set it to the alt-text initially

After that the user can update it however they wish ( or remove it entirely ):

function add_image_alt( $attachment_id ) {
    $image_mimes     = array( 'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon' );
    $attachment_type = get_post_mime_type( $attachment_id );

    if( in_array( $attachment_type, $image_mimes ) ) {
        $attachment_title = get_the_title( $attachment_id );
        update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $attachment_title ) );
    }
}
add_action( 'add_attachment', 'add_image_alt' );
1
  • Thanks very much, and it's a good approach for managing alt text. But the main thing I need, is to require and/or validate user input; in other words, I don't want users to be able to update it however they wish
    – allanberry
    May 5, 2016 at 17:08

Your Answer

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

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