Skip to content

Instantly share code, notes, and snippets.

@arunbasillal
Last active December 15, 2022 07:54
Show Gist options
  • Save arunbasillal/fb96cd8828a1af18b2bcdeb80459a1f7 to your computer and use it in GitHub Desktop.
Save arunbasillal/fb96cd8828a1af18b2bcdeb80459a1f7 to your computer and use it in GitHub Desktop.
Use Captions as alt text.
/**
* Use Captions as alt text.
*
* @param $attributes (array) Associative array of image attributes.
* @param $image_id (int) ID of the current image.
* @param $parent_post_id (int) ID of the post the image is inserted into. 0 for images not attached to a post.
*
* @author Arun Basil Lal
* @link https://imageattributespro.com/codex/iaffpro_image_attributes/
*/
function prefix_use_captions_as_alt_text( $attributes, $image_id, $parent_post_id ) {
$image = get_post( $image_id );
// Image caption is saved as post_excerpt.
if ( ! empty( $image->post_excerpt ) ) {
$attributes['alt_text'] = $image->post_excerpt;
}
return $attributes;
}
add_filter( 'iaffpro_image_attributes', 'prefix_use_captions_as_alt_text', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment