get_post_galleries( int|WP_Post $post, bool $html = true ): array
- Since
- 3.6.0
- Source
wp-includes/media.php:5208
Compatibility
- WordPress
- since 3.6.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
$postint|WP_Post- Post ID or object.
$htmlbooloptional- Whether to return HTML or data in the array. Default true.Default:
true
Return value
array- A list of arrays, each containing gallery data and srcs parsed from the expanded shortcode.
Performance profile
How much work a call to get_post_galleries() 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
- Scales with input
- Instructions
- 8–62
- Plugin surface
- 1 hook
- Called by
- 5
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 245.
Third-party callbacks on 'get_post_galleries' run inside this call, and their cost is not bounded by anything here.
5 places 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()called directly - regexregular expression over the whole input
preg_match_all()called directly
Further down the call graph this can also reach cache, serialize, option 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_post_galleries() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8 | get_post() |
!has_shortcode() && !has_block() | 22 | get_post(), has_shortcode(), has_block() |
has_shortcode() && !has_block() | 40–43 | get_post(), has_shortcode(), get_shortcode_regex(), preg_match_all(), has_block(), apply_filters() |
!has_shortcode() | 47–50 | get_post(), has_shortcode(), has_block(), get_shortcode_regex(), preg_match_all(), has_block(), apply_filters() |
has_shortcode() && has_block() | 52–55 | get_post(), has_shortcode(), get_shortcode_regex(), preg_match_all(), has_block(), parse_blocks(), array_shift(), apply_filters() |
!has_shortcode() && has_block() | 59–62 | get_post(), has_shortcode(), has_block(), get_shortcode_regex(), preg_match_all(), has_block(), parse_blocks(), array_shift(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 245 | 8–62 | 36 | |
| 8.5 | 245 | 8–62 | 36 | |
| 8.4 | 245 | 8–62 | 36 | 18 fewer instructions than PHP 8.3 |
| 8.3 | 263 | 8–62 | 36 | |
| 8.2 | 263 | 8–62 | 36 | |
| 8.1 | 263 | 8–62 | 36 | |
| 7.4 | 263 | 8–62 | 36 |
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.
Hooks and filters fired · 1
One hook fires while get_post_galleries() runs, in this order:
- apply_filters( get_post_galleries )filterline 5354 (+146 into the body)
Filters the list of all found galleries in the given post.
Uses · 10
- get_post()Retrieves post data given a post ID or post object.
- has_shortcode()Determines whether the passed content contains the specified shortcode.
- has_block()Determines whether a $post or a string contains a specific block type.
- get_shortcode_regex()Retrieves the shortcode regular expression for searching.
- shortcode_parse_atts()Retrieves all attributes from the shortcodes tag.
- do_shortcode_tag()Regular Expression callable for do_shortcode() for calling shortcode hook.
- parse_blocks()Parses blocks out of a content string.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- wp_get_attachment_url()Retrieves the URL for an attachment.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 5
- Twenty_Fourteen_Ephemera_Widget::widget()Output the HTML for this widget.
- get_post_galleries_images()Retrieves the image srcs from galleries from a post's content, if present.
- get_post_gallery()Checks a specified post's content for gallery and, if present, return the first
- twentyeleven_get_gallery_images()Retrieve the IDs for images in a gallery.
- twentyten_get_gallery_images()Retrieve the IDs for images in a gallery.
Source code
function get_post_galleries( $post, $html = true ) { $post = get_post( $post ); if ( ! $post ) { return array(); } if ( ! has_shortcode( $post->post_content, 'gallery' ) && ! has_block( 'gallery', $post->post_content ) ) { return array(); } $galleries = array(); if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) { foreach ( $matches as $shortcode ) { if ( 'gallery' === $shortcode[2] ) { $srcs = array(); $shortcode_attrs = shortcode_parse_atts( $shortcode[3] ); // Specify the post ID of the gallery we're viewing if the shortcode doesn't reference another post already. if ( ! isset( $shortcode_attrs['id'] ) ) { $shortcode[3] .= ' id="' . (int) $post->ID . '"'; } $gallery = do_shortcode_tag( $shortcode ); if ( $html ) { $galleries[] = $gallery; } else { preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER ); if ( ! empty( $src ) ) { foreach ( $src as $s ) { $srcs[] = $s[2]; } } $galleries[] = array_merge( $shortcode_attrs, array( 'src' => array_values( array_unique( $srcs ) ), ) ); } } } } if ( has_block( 'gallery', $post->post_content ) ) { $post_blocks = parse_blocks( $post->post_content ); while ( $block = array_shift( $post_blocks ) ) { $has_inner_blocks = ! empty( $block['innerBlocks'] ); // Skip blocks with no blockName and no innerHTML. if ( ! $block['blockName'] ) { continue; } // Skip non-Gallery blocks. if ( 'core/gallery' !== $block['blockName'] ) { // Move inner blocks into the root array before skipping. if ( $has_inner_blocks ) { array_push( $post_blocks, ...$block['innerBlocks'] ); } continue; } // New Gallery block format as HTML. if ( $has_inner_blocks && $html ) { $block_html = wp_list_pluck( $block['innerBlocks'], 'innerHTML' ); $galleries[] = '<figure>' . implode( ' ', $block_html ) . '</figure>'; continue; } $srcs = array(); // New Gallery block format as an array. if ( $has_inner_blocks ) { $attrs = wp_list_pluck( $block['innerBlocks'], 'attrs' ); $ids = wp_list_pluck( $attrs, 'id' );Changelog
Introduced in 3.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/media.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.