wppaste
WordPress

get_post_galleries( int|WP_Post $post, bool $html = true ): array

Since
3.6.0
Source
wp-includes/media.php:5258
Retrieves galleries from the passed post's content.

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

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–62

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 245.

Plugin surface
1 hook

Third-party callbacks on 'get_post_galleries' run inside this call, and their cost is not bounded by anything here.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • regexregular expression over the whole inputpreg_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.

WhenInstructionsCalls it makes
always8get_post()
!has_shortcode() && !has_block()22get_post(), has_shortcode(), has_block()
has_shortcode() && !has_block()40–43get_post(), has_shortcode(), get_shortcode_regex(), preg_match_all(), has_block(), apply_filters()
!has_shortcode()47–50get_post(), has_shortcode(), has_block(), get_shortcode_regex(), preg_match_all(), has_block(), apply_filters()
has_shortcode() && has_block()52–55get_post(), has_shortcode(), get_shortcode_regex(), preg_match_all(), has_block(), parse_blocks(), array_shift(), apply_filters()
!has_shortcode() && has_block()59–62get_post(), has_shortcode(), has_block(), get_shortcode_regex(), preg_match_all(), has_block(), parse_blocks(), array_shift(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2458–6236
8.52458–6236
8.42458–623618 fewer instructions than PHP 8.3
8.32638–6236
8.22638–6236
8.12638–6236
7.42638–6236

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:

  1. apply_filters( get_post_galleries )filterline 5404 (+146 into the body)

    Filters the list of all found galleries in the given post.

Uses · 10

Used by · 5

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.

  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.

About this page

Parsed data
Generated from the wordpress-develop 6.9.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.