I try to add a custom attribute to every image which was placed with the Gutenberg Editor.
What I need for each image
<img src="#" data-custom="image-caption" />
What I tried
add_filter( 'wp_get_attachment_image_attributes', 'add_custom_image_data_attributes', 10, 3 );
function add_custom_image_data_attributes( $attr, $attachment, $size ) {
$attr['data-custom'] = 'My image caption';
return $attr;
}
But this has no effect. And how could I retrieve the caption for the specific image as variable?