wppaste
WordPress

block_core_gallery_render( array $attributes, string $content, array $block ): string

Since
6.0.0
Source
wp-includes/blocks/gallery.php:309
Renders the core/gallery block on the server.

Compatibility

WordPress
since 6.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attributesarray
Attributes of the block being rendered.
$contentstring
Content of the block being rendered.
$blockarray
The block instance being rendered.

Return value

string
The content of the block being rendered.

Performance profile

How much work a call to block_core_gallery_render() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5

Executed per call on PHP 8.5. The body compiles to 392.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_post()one call below block_core_gallery_render()
  • cacheobject cachewp_cache_get()one call below block_core_gallery_render()

Further down the call graph this can also reach hook, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost block_core_gallery_render() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev3895553 fewer instructions than PHP 8.5
8.5392555
8.4392555
8.3392555
8.2392555
8.13925557 fewer instructions than PHP 7.4
7.4399555

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 13

Show all 13

Source code

function block_core_gallery_render( $attributes, $content, $block ) {	static $global_styles = null; 	// In dynamic mode the gallery's images are resolved at render time instead of	// being authored as inner blocks, so `save.js` persists at most the	// gallery-level caption — a bare `<figcaption>`, or nothing when there is no	// caption. Resolve the configured source to a list of attachments, render an	// image block for each, and build the gallery `<figure>` wrapper from scratch.	// The gap/randomOrder/lightbox post-processing below then runs over the	// constructed markup unchanged.	if ( ! empty( $attributes['dynamicContent'] ) ) {		$attachment_ids = block_core_gallery_resolve_dynamic_source( $attributes['dynamicContent'], $block ); 		// Nothing resolved — no attachments, or an unrecognized source. Render		// nothing rather than an empty gallery wrapper; a saved caption is		// meaningless without images, so it is intentionally dropped too.		if ( empty( $attachment_ids ) ) {			return '';		} 		// The source query only fetched IDs (`fields => ids`), which skips		// WP_Query's cache priming. Each image rendered below reads the		// attachment post and its meta (via `wp_get_attachment_image()`,		// `get_post()`, etc.), so warm the post and meta caches in a single pair		// of queries up front instead of paying ~two queries per attachment.		// Term cache is left cold: the render path doesn't read attachment terms.		if ( count( $attachment_ids ) > 1 ) {			_prime_post_caches( $attachment_ids, false, true );		} 		// Expose the gallery's provided context (plus galleryId/postId/postType)		// to each image block, since these images are rendered outside the		// gallery's real inner-block tree.		$image_context = array_merge(			is_array( $block->context ) ? $block->context : array(),			array(				'allowResize'          => $attributes['allowResize'] ?? false,				'imageCrop'            => $attributes['imageCrop'] ?? true,				'fixedHeight'          => $attributes['fixedHeight'] ?? true,				'navigationButtonType' => $attributes['navigationButtonType'] ?? 'icon',			)		); 		$images_markup = '';		foreach ( $attachment_ids as $attachment_id ) {			$images_markup .= block_core_gallery_render_dynamic_image( $attachment_id, $attributes, $image_context );		} 		// Build the wrapper rather than parsing/splicing saved markup.		// `get_block_wrapper_attributes()` supplies the block-support		// classes/styles (align, color, border, spacing, anchor id); the layout		// render filter adds the flex layout classes downstream — the same way a		// static gallery's wrapper is composed (`useBlockProps.save()` plus that		// filter). Only the gallery-specific classes are added explicitly, and		// they mirror `save.js` (kept in sync deliberately — see that file).		$gallery_classes  = 'wp-block-gallery has-nested-images';		$gallery_classes .= isset( $attributes['columns'] )			? ' columns-' . (int) $attributes['columns']			: ' columns-default';		if ( $attributes['imageCrop'] ?? true ) {			$gallery_classes .= ' is-cropped';		}		$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $gallery_classes ) ); 		// In dynamic mode `save.js` persists only the gallery-level caption, so		// `$content` is the saved `<figcaption>` (or empty). Append it after the		// resolved images — matching the static gallery's `{images}{caption}`		// order — without parsing it.		$content = sprintf( '<figure %s>%s%s</figure>', $wrapper_attributes, $images_markup, $content );	} 	// Adds a style tag for the --wp--style--unstable-gallery-gap var.	// The Gallery block needs to recalculate Image block width based on	// the current gap setting in order to maintain the number of flex columns	// so a css var is added to allow this. 	$style_attr = is_array( $attributes['style'] ?? null )		? $attributes['style']		: array();	if (

Changelog

Introduced in 6.0.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.0.4
Parameter $block added.verified against source
6.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/blocks/gallery.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.