method_get_lightbox_attributes()
method_get_lightbox_attributes( $attachment_id )
The attributes that make an <a> a lightbox trigger for an attachment, shared by the Fitted Image and Swiper Gallery blocks.
Description
Returns the attributes a block puts on the link around an image so the shared lightbox script (lib/blocks/utils/lightbox.js, bundled into each block’s view script) opens the uncropped, full-size image in its <dialog class="method-lightbox">. The href is that same full-size file, so without the script, or in a browser without <dialog>, the element degrades to a plain link to the image. How the lightbox itself works is on the Image Lightbox page.
Parameters
$attachment_id (int) :
The attachment to show in the lightbox.
Return
(array) Attribute name ⇒ value pairs, unescaped — pass them through get_block_wrapper_attributes() or escape them yourself — or an empty array if the attachment has no usable full-size image.
| Attribute | Value |
|---|---|
href | URL of the full-size image. |
data-method-lightbox | 1 |
data-method-lightbox-src | The same URL. |
data-method-lightbox-width | Full-size width in px. |
data-method-lightbox-height | Full-size height in px. |
data-method-lightbox-alt | The attachment’s alt text (_wp_attachment_image_alt), trimmed; may be empty. |
data-method-lightbox-srcset | wp_get_attachment_image_srcset( $id, 'full' ), only when WordPress returns one. |
aria-haspopup | dialog |
Source
File: lib/blocks.php
Usage
$lightbox = method_get_lightbox_attributes( $attachment_id );
if ( $lightbox ) {
$attrs = get_block_wrapper_attributes( array_merge( array( 'class' => 'method-fitted-image method-lightbox-trigger' ), $lightbox ) );
$output = '<a ' . $attrs . '>' . $image . '</a>';
} else {
$output = '<div ' . get_block_wrapper_attributes( array( 'class' => 'method-fitted-image' ) ) . '>' . $image . '</div>';
}
Notes
- Called by Fitted Image when its Enable Lightbox option is on (the whole block becomes the trigger) and by Swiper Gallery for every slide when Open images in a lightbox is on.
- The script reads the
data-method-lightbox-*attributes, not thehref, so a block may point the link somewhere else and still open the lightbox. - The full-size check uses
wp_get_attachment_image_src( $id, 'full' ); an attachment that is not an image, or that has been deleted, yields the empty array and the caller should render a non-link wrapper.
Changelog
| Version | Description |
|---|---|
| 2.0.0-beta23 | Introduced, with the shared lightbox (eacd345). |