diff --git a/modules/carousel/jetpack-carousel.php b/modules/carousel/jetpack-carousel.php
index 0bce42d91d4f3..fd3e9e4f1ad59 100644
--- a/modules/carousel/jetpack-carousel.php
+++ b/modules/carousel/jetpack-carousel.php
@@ -540,9 +540,59 @@ class_exists( 'Jetpack_AMP_Support' )
* @param array $extra_data Array of data about the site and the post.
*/
$extra_data = apply_filters( 'jp_carousel_add_data_to_container', $extra_data );
+
foreach ( (array) $extra_data as $data_key => $data_values ) {
- $html = str_replace( '
loadHTML( $html ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
+ libxml_use_internal_errors( $old_libxml_use_internal_errors );
+ libxml_disable_entity_loader( $old_libxml_disable_entity_loader );
+
+ // Let's look for lists and divs.
+ $ul_tags = $dom_doc->getElementsByTagName( 'ul' );
+ $div_tags = $dom_doc->getElementsByTagName( 'div' );
+
+ // Loop through each ul, and add the data to it if it is a gallery block.
+ foreach ( $ul_tags as $ul_tag ) {
+ if ( false !== strpos( $ul_tag->getAttribute( 'class' ), 'wp-block-gallery' ) ) {
+ $ul_tag->setAttribute(
+ $data_key,
+ wp_json_encode( $data_values )
+ );
+ }
+ }
+
+ /*
+ * Loop through each div and add the data, only when it's a gallery block div.
+ * We want to avoid adding data to divs like wp-block-columns.
+ * We do however want data on divs like wp-block-jetpack-tiled-gallery.
+ */
+ foreach ( $div_tags as $div_tag ) {
+ if (
+ false === strpos( $div_tag->getAttribute( 'class' ), 'wp-block-' )
+ || false !== strpos( $div_tag->getAttribute( 'class' ), 'gallery' )
+ ) {
+ $div_tag->setAttribute(
+ $data_key,
+ wp_json_encode( $data_values )
+ );
+ }
+ }
+
+ // Save our updated HTML.
+ $html = $dom_doc->saveHTML();
}
}