A
A
Alexey selftrips.ru2017-10-19 11:19:08
Images
Alexey selftrips.ru, 2017-10-19 11:19:08

How can I automatically add alt to images when they are uploaded?

That is, when loading, and not on the fly, for example, by the title of the entry into which the image is loaded.
What would then be the opportunity to edit it (alt).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2017-10-19
@selftrips

function set_image_alt_on_upload( $post_ID ) {

  if ( wp_attachment_is_image( $post_ID ) ) {
    
    $attachment = get_post( $post_ID );
    $parent_post = get_post( $attachment->post_parent );

    update_post_meta( $post_ID, '_wp_attachment_image_alt', $parent_post->post_title );
  } 
}
add_action( 'add_attachment', 'set_image_alt_on_upload' );

I wrote from my head and did not test it, but it should work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question