block_core_gallery_render_dynamic_image( int $attachment_id, array $attributes, array $context ): string
- Since
- 7.0.0
- Source
wp-includes/blocks/gallery.php:225
core/image block for a Gallery block running in dynamic mode, applying the gallery-wide settings that affect how an image renders.Description
The image markup is generated here (via wp_get_attachment_image()) and rendered through a real core/image block instance so that the image block's own render callback and lightbox behavior run, and so the gallery's existing lightbox/interactivity post-processing can pick it up.
Compatibility
- WordPress
- since 7.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 1 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$attachment_idint- The image attachment ID.
$attributesarray- The gallery block attributes.
$contextarray- Context to expose to the inner image block.
Return value
string- The rendered image block HTML, or an empty string on failure.
Performance profile
How much work a call to block_core_gallery_render_dynamic_image() 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
- Scaling
- Constant
- Instructions
- 21–127
- Plugin surface
- None
- Called by
- 1
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 131.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()one call below block_core_gallery_render_dynamic_image()
Further down the call graph this can also reach option, cache, 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 · 18 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost block_core_gallery_render_dynamic_image() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 21–25 | wp_get_attachment_image() |
$aspect_ratio !== "auto" | 31–33 | safecss_filter_attr(), wp_get_attachment_image() |
empty($image_attributes) && $caption === "" | 68–79 | wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), get_post(), esc_attr(), sprintf(), ->render() |
empty($image_attributes) && $caption !== "" | 75–86 | wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render() |
$aspect_ratio !== "auto" && empty($image_attributes) && $caption === "" | 78–87 | safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), get_post(), esc_attr(), sprintf(), ->render() |
$aspect_ratio !== "auto" && empty($image_attributes) && $caption !== "" | 85–94 | safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render() |
!empty($image_attributes) && !isset($image_attributes) && $caption === "" | 87–98 | wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), sprintf(), get_post(), esc_attr(), sprintf(), ->render() |
!empty($image_attributes) && !isset($image_attributes) && $caption !== "" | 94–105 | wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render() |
!empty($image_attributes) && $caption === "" | 94–105 | wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), sprintf(), get_post(), esc_attr(), sprintf(), ->render() |
$aspect_ratio !== "auto" && !empty($image_attributes) && !isset($image_attributes) && $caption === "" | 97–106 | safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), sprintf(), get_post(), esc_attr(), sprintf(), ->render() |
!empty($image_attributes) && $caption !== "" | 101–112 | wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render() |
!empty($image_attributes) && isset($image_attributes) && $caption === "" | 101–112 | wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), esc_attr(), sprintf(), get_post(), esc_attr(), sprintf(), ->render() |
6 further outcomes, up to 127 instructions
$aspect_ratio !== "auto" && !empty($image_attributes) && !isset($image_attributes) && $caption !== "" | 104–113 | safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render() |
$aspect_ratio !== "auto" && !empty($image_attributes) && $caption === "" | 104–113 | safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), sprintf(), get_post(), esc_attr(), sprintf(), ->render() |
!empty($image_attributes) && isset($image_attributes) && $caption !== "" | 108–119 | wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), esc_attr(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render() |
$aspect_ratio !== "auto" && !empty($image_attributes) && $caption !== "" | 111–120 | safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render() |
$aspect_ratio !== "auto" && !empty($image_attributes) && isset($image_attributes) && $caption === "" | 111–120 | safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), esc_attr(), sprintf(), get_post(), esc_attr(), sprintf(), ->render() |
$aspect_ratio !== "auto" && !empty($image_attributes) && isset($image_attributes) && $caption !== "" | 118–127 | safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), esc_attr(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 131 | 21–127 | 12 | |
| 8.5 | 131 | 21–127 | 12 | |
| 8.4 | 131 | 21–127 | 12 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 133 | 21–129 | 12 | |
| 8.2 | 133 | 21–129 | 12 | |
| 8.1 | 133 | 21–129 | 12 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 136 | 21–132 | 12 |
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 · 9
- safecss_filter_attr()Filters an inline style attribute and removes disallowed rules.
- wp_get_attachment_image()Gets an HTML img element representing an image attachment.
- block_core_gallery_dynamic_image_link_attributes()Builds the link-related image block attributes for a dynamically rendered gallery image, mapping the gallery-wide `linkTo` setting onto a single image.
- esc_url()Checks and cleans a URL.
- esc_attr()Escaping for HTML attributes.
- get_post()Retrieves post data given a post ID or post object.
- wp_kses_post()Sanitizes content for allowed HTML tags for post content.
- new WP_Block($image_block, $context)::render()
- WP_Block::__construct()Constructor.
Used by · 1
- block_core_gallery_render()Renders the `core/gallery` block on the server.
Source code
function block_core_gallery_render_dynamic_image( $attachment_id, $attributes, $context ) { $size_slug = $attributes['sizeSlug'] ?? 'large'; $aspect_ratio = $attributes['aspectRatio'] ?? 'auto'; $img_attr = array( 'class' => 'wp-image-' . $attachment_id ); if ( $aspect_ratio && 'auto' !== $aspect_ratio ) { // Run the aspect ratio through the same sanitization used for every other // block inline style, so an unsafe value can't break out of the style // attribute or inject additional markup. $img_attr['style'] = safecss_filter_attr( sprintf( 'aspect-ratio:%s;object-fit:cover;', $aspect_ratio ) ); } $image_markup = wp_get_attachment_image( $attachment_id, $size_slug, false, $img_attr ); if ( ! $image_markup ) { return ''; } $image_attributes = array_merge( array( 'id' => $attachment_id, 'data-id' => (string) $attachment_id, 'sizeSlug' => $size_slug, ), block_core_gallery_dynamic_image_link_attributes( $attachment_id, $attributes ) ); if ( $aspect_ratio && 'auto' !== $aspect_ratio ) { $image_attributes['aspectRatio'] = $aspect_ratio; $image_attributes['scale'] = 'cover'; } // Wrap in a link when the gallery links images somewhere. if ( ! empty( $image_attributes['href'] ) ) { $image_markup = sprintf( '<a href="%1$s"%2$s%3$s>%4$s</a>', esc_url( $image_attributes['href'] ), isset( $image_attributes['linkTarget'] ) ? ' target="' . esc_attr( $image_attributes['linkTarget'] ) . '"' : '', isset( $image_attributes['rel'] ) ? ' rel="' . esc_attr( $image_attributes['rel'] ) . '"' : '', $image_markup ); } // Use the raw caption (`post_excerpt`) so the frontend mirrors the editor // preview, which builds the caption from the REST `caption.raw` value. Gap: // the REST API exposes no caption run through `wp_get_attachment_caption`, so // that filter isn't applied here either. $attachment = get_post( $attachment_id ); $caption = $attachment ? $attachment->post_excerpt : ''; if ( '' !== $caption ) { $image_markup .= sprintf( '<figcaption class="wp-element-caption">%s</figcaption>', wp_kses_post( $caption ) ); } $figure = sprintf( '<figure class="wp-block-image size-%1$s">%2$s</figure>', esc_attr( $size_slug ), $image_markup ); $image_block = array( 'blockName' => 'core/image', 'attrs' => $image_attributes, 'innerBlocks' => array(), 'innerHTML' => $figure, 'innerContent' => array( $figure ), ); return ( new WP_Block( $image_block, $context ) )->render();}Changelog
Introduced in 7.0.0.
Signature, return type and hooks compared across 1 parsed release.
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.