Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Generate the image alt text on upload
- * Sets image alt text
- * - https://codex.wordpress.org/Plugin_API/Action_Reference/add_attachment
- * - https://developer.wordpress.org/reference/hooks/add_attachment/
- *
- * @param Integer $post_id The Attachment ID
- *
- * @return void
- */
- function wp_set_image_alt( $post_id ) {
- // If we're not dealing with an image
- if( ! wp_attachment_is_image( $post_id ) ) {
- return;
- }
- $title = get_post_field( 'post_title', $post_id );
- if( ! empty( $title ) && ! is_wp_error( $title ) ) {
- update_post_meta( $post_id, '_wp_attachment_image_alt', sanitize_text_field( $title ) );
- }
- }
- add_action( 'add_attachment', 'wp_set_image_alt' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement