Remove lazy loading from a WordPress gallery block

Here is a short snippet of code which removes the lazy loading attribute on the all the images in a WordPress gallery block when the block is assigned a class of is-style-carousel.

 * Removes lazy loading from the gallery block.
 * When the gallery block style is for a carousel.
 * @param string $block_content The block content.
 * @param array  $block         The block data.
function hd_remove_gallery_carousel_lazy_loading( $block_content, $block ) {

	// if this block does not have the carousel style.
	if ( strpos( $block['attrs']['className'], 'is-style-carousel' ) === false ) {
		return $block_content;

	// string replace the block content to remove the lazy loading.
	$block_content = str_replace( 'loading="lazy"', '', $block_content );
	return $block_content;


add_filter( 'render_block_core/gallery', 'hd_remove_gallery_carousel_lazy_loading', 10, 2 );