0

I try to get gutenberg media gallery block output, and want to use default figcaption text feature for images titles (for js lightbox). How can i get this property figcaption text value?

add_filter( 'render_block', function( $block_content, $block ) {
    if ( 'core/gallery' !== $block['blockName'] || ! isset( $block['attrs']['ids'] ) ) {
           return $block_content;
    }
        $li = '';

    foreach( (array) $block['attrs']['ids'] as $id ) {
        $li .= sprintf( '<li><a href="%s" data-lightbox="photo" data-title="%s"><img src="%s" class="w-100"></a></li>',
        wp_get_attachment_image_src( $id, '' )[0],
        'FIGCAPTION TEXT',
        wp_get_attachment_image_src( $id, 'thumbnail' )[0] );

    }
    return sprintf( '<ul id="gallery-grid">%s</ul>', $li ); 
}, 10, 2 );

2 Answers 2

1

You’re right...the figcaptions are encoded as html in $block['innerHTML']. You could do sth. like $tmpArray = explode('</li>',$block['innerHTML']); before your foreach loop to split the HTML string into an array that matches your gallery items and inside the loop strip_tags($tmpArray[i]); to strip away all html tags and only get the text string inside <figcaption></figcaption>. You’ll only need to add a counter i to your loop to have an index for the $tmpArray.

1
  • Thank for idea! Seems to no direct methods to get this value by property, only html parse.
    – Mike
    Jun 18, 2019 at 16:05
0

Try wp_get_attachment_caption( $id ) for caption and get_post_meta( $id, '_wp_attachment_image_alt', true ) for alt-text

1
  • Thank for your reply! Unfortunately this return internal image properties, not from media gallery gutenberg block.
    – Mike
    Jun 17, 2019 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.