Skip to content

Instantly share code, notes, and snippets.

@iworks
Created September 12, 2022 10:39
Show Gist options
  • Save iworks/f846d14df92b843da315e5002baff6d0 to your computer and use it in GitHub Desktop.
Save iworks/f846d14df92b843da315e5002baff6d0 to your computer and use it in GitHub Desktop.
Add post title as a alt for thumbnail
<?php
add_filter(
'wp_get_attachment_image_attributes',
function( $attr, $attachment, $size ) {
if ( ! is_singular() ) {
return $attr;
}
if ( isset( $attr['alt'] ) && ! empty( $attr['alt'] ) ) {
return $attr;
}
$attr['alt'] = get_the_title();
return $attr;
},
10,
3
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment