Last active
May 10, 2022 14:23
-
-
Save krasenslavov/bbb1d3885205d0b5b0938023d4239321 to your computer and use it in GitHub Desktop.
Overwrite and extend Gutenberg gallery block. Visit blog post https://bit.ly/34OMvdz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('init', function() { | |
register_block_type('core/gallery', array( | |
'render_callback' => function($attributes, $content) { | |
$items = count($attributes['ids']); | |
$new_content = "<figure class='wp-block-gallery columns-{$items}'><ul class='blocks-gallery-grid'>"; | |
foreach($attributes['ids'] as $idx => $attachment_id) { | |
if (empty($attributes['sizeSlug'])) { | |
$attributes['sizeSlug'] = 'full'; | |
} | |
$src = wp_get_attachment_url($attachment_id); | |
$srcset = wp_get_attachment_image_srcset($attachment_id, $attributes['sizeSlug'], null); | |
$sizes = wp_get_attachment_image_sizes($attachment_id, $attributes['sizeSlug'], null); | |
$new_content .= "<li class='blocks-gallery-item'><figure><input type='checkbox' name='wp-image-{$attachment_id}' id='wp-image-{$attachment_id}' /><div class='gallery-item-modal'><label for='wp-image-{$attachment_id}'><img src='{$src}' alt='' /></label></div><label for='wp-image-{$attachment_id}'><img src='{$src}' data-id='{$attachment_id}' data-full-url='{$src}' data-link='' srcset='{$srcset}' sizes='{$sizes}' class='wp-image-{$attachment_id}' alt='' /></label></figure></li>"; | |
} | |
$new_content .= '</ul></figure>'; | |
return $new_content; | |
}, | |
)); | |
}, 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment