wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



block_core_gallery_dynamic_image_link_attributes › WordPress Function

Seit7.0.0
Veraltetn/v
block_core_gallery_dynamic_image_link_attributes ( $attachment_id, $attributes )
Parameter: (2)
  • (int) $attachment_id The image attachment ID.
    Erforderlich: Ja
  • (array) $attributes The gallery block attributes.
    Erforderlich: Ja
Gibt zurück:
  • (array) Partial image block attributes (`href`, `linkDestination`,
    `linkTarget`, `rel`, `lightbox`).
Definiert in:
Codex:

Builds the link-related image block attributes for a dynamically rendered gallery image, mapping the gallery-wide `linkTo` setting onto a single image.

Mirrors the editor's getHrefAndDestination() (see gallery/utils.js).


Quellcode

function block_core_gallery_dynamic_image_link_attributes( $attachment_id, $attributes ) {
	$link_to = $attributes['linkTo'] ?? 'none';
	$attrs   = array();

	switch ( $link_to ) {
		// Gutenberg uses 'media'/'attachment'; WP Core uses 'file'/'post'.
		case 'media':
		case 'file':
			$attrs['href']            = wp_get_attachment_url( $attachment_id );
			$attrs['linkDestination'] = 'media';
			break;
		case 'attachment':
		case 'post':
			$attrs['href']            = get_attachment_link( $attachment_id );
			$attrs['linkDestination'] = 'attachment';
			break;
		case 'lightbox':
			$attrs['linkDestination'] = 'none';
			$attrs['lightbox']        = array( 'enabled' => true );
			break;
	}

	if ( ! empty( $attrs['href'] ) && '_blank' === ( $attributes['linkTarget'] ?? '' ) ) {
		$attrs['linkTarget'] = '_blank';
		$attrs['rel']        = 'noopener';
	}

	return $attrs;
}