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.

AttributeValue
hrefURL of the full-size image.
data-method-lightbox1
data-method-lightbox-srcThe same URL.
data-method-lightbox-widthFull-size width in px.
data-method-lightbox-heightFull-size height in px.
data-method-lightbox-altThe attachment’s alt text (_wp_attachment_image_alt), trimmed; may be empty.
data-method-lightbox-srcsetwp_get_attachment_image_srcset( $id, 'full' ), only when WordPress returns one.
aria-haspopupdialog

Source

File: lib/blocks.php


Usage

PHP
$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 the href, 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

VersionDescription
2.0.0-beta23Introduced, with the shared lightbox (eacd345).