Last active
November 4, 2022 21:08
-
-
Save vralle/e8111a4a3322bd0ebcb0a40e10db62bb to your computer and use it in GitHub Desktop.
Custom Gallery Shortcode output
This file contains 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 | |
/** | |
* Force the Gallery Shortcode attributes | |
*/ | |
add_filter( | |
'shortcode_atts_gallery', | |
function ($out) { | |
$out['size'] = 'medium'; | |
return $out; | |
}, | |
11 | |
); | |
add_filter('post_gallery', 'vralle_by_may_be_amp_gallery', 11, 2); | |
function vralle_by_may_be_amp_gallery($output, $attr) | |
{ | |
/** | |
* Force Gallery Shortcode attributes for AMP | |
* @link https://github.com/ampproject/amp-wp/blob/develop/includes/embeds/class-amp-gallery-embed.php | |
*/ | |
// remove_filter('post_gallery', 'vralle_by_may_be_amp_gallery', 11); | |
// $attr['lightbox'] = true; | |
// $attr['amp-carousel'] = false; | |
// $output = gallery_shortcode($attr); | |
// add_filter('post_gallery', 'vralle_by_may_be_amp_gallery', 11, 2); | |
$output = vralle_by_gallery($attr); | |
return $output; | |
} | |
function vralle_by_gallery($attr) | |
{ | |
$post = get_post(); | |
static $instance = 0; | |
$instance++; | |
$atts = shortcode_atts( | |
array( | |
'order' => 'ASC', | |
'orderby' => 'menu_order ID', | |
'include' => '', | |
'exclude' => '', | |
'id' => $post ? $post->ID : 0, | |
'size' => 'medium', | |
'link' => 'large', | |
'columns' => 1, | |
), | |
$attr, | |
'gallery' | |
); | |
$id = intval($atts['id']); | |
if (!empty($atts['include'])) { | |
$_attachments = get_posts( | |
array( | |
'include' => $atts['include'], | |
'post_status' => 'inherit', | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'order' => $atts['order'], | |
'orderby' => $atts['orderby'] | |
) | |
); | |
$attachments = array(); | |
foreach ($_attachments as $key => $val) { | |
$attachments[$val->ID] = $_attachments[$key]; | |
} | |
} elseif (!empty($atts['exclude'])) { | |
$attachments = get_children( | |
array( | |
'post_parent' => $id, | |
'exclude' => $atts['exclude'], | |
'post_status' => 'inherit', | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'order' => $atts['order'], | |
'orderby' => $atts['orderby'] | |
) | |
); | |
} else { | |
$attachments = get_children( | |
array( | |
'post_parent' => $id, | |
'post_status' => 'inherit', | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'order' => $atts['order'], | |
'orderby' => $atts['orderby'] | |
) | |
); | |
} | |
if (empty($attachments)) { | |
return ''; | |
} | |
if (is_feed()) { | |
$output = "\n"; | |
foreach ($attachments as $att_id => $attachment) { | |
$output .= wp_get_attachment_link($att_id, $atts['size'], true) . "\n"; | |
} | |
return $output; | |
} | |
$columns = intval($atts['columns']); | |
$selector = "gallery-{$instance}"; | |
$size_class = sanitize_html_class($atts['size']); | |
$gallery_div = sprintf( | |
'<div id="%s" class="gallery gallery--columns-%s gallery--size-%s">', | |
$selector, | |
$columns, | |
$size_class | |
); | |
/** | |
* Filters the default gallery shortcode CSS styles. | |
* | |
* @param string Default CSS styles and opening HTML div container | |
* for the gallery shortcode output. | |
*/ | |
$output = apply_filters('gallery_style', $gallery_div); | |
$i = 0; | |
foreach ($attachments as $id => $attachment) { | |
// Setup the image attributtes | |
$image_attributes = array( | |
'lightbox' => '', | |
); | |
// Detect caption | |
$is_caption = trim($attachment->post_excerpt) ? true : false; | |
// Setup image ARIA if caption presents | |
if ($is_caption) { | |
$caption_id = $selector . '-' . $id; | |
$image_attributes['aria-describedby'] = $caption_id; | |
} | |
$output .= '<figure class="gallery__figure figure">'; | |
// Get image tag | |
$image_output = wp_get_attachment_image($id, $atts['size'], false, $image_attributes); | |
// Setup link | |
$image_url = ''; | |
if (!empty($atts['link']) && !vralle_by_is_amp()) { | |
if ('post' === $atts['link']) { | |
$image_url = get_attachment_link($id); | |
} elseif ('none' !== $atts['link']) { | |
$image_url = wp_get_attachment_image_url($id, $atts['link']); | |
} | |
} | |
// Place image tag to link, if link presents | |
if (!!$image_url) { | |
$image_output = sprintf( | |
'<a class="figure__link" href="%s">%s</a>', | |
$image_url, | |
$image_output | |
); | |
} | |
$output .= $image_output; | |
// Setup caption | |
if ($is_caption) { | |
$output .= sprintf( | |
'<figcaption id="%s" class="figure__caption">%s</figcaption>', | |
$caption_id, | |
wptexturize($attachment->post_excerpt) | |
); | |
} | |
$output .= '</figure>'; | |
} | |
$output .= '</div>'; | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great work! Thanks for sharing.