wppaste
WordPress

gallery_shortcode( array $attr ): string

Since
2.5.0, 2.8.0, 2.9.0, 3.5.0, 3.6.0, 3.7.0, 3.9.0, 4.0.0, 4.1.0, 4.2.0, 4.6.0, 5.1.0, 5.3.0, 5.5.0, 5.6.0
Source
wp-includes/media.php:2685
Builds the Gallery shortcode output.

Description

This implements the functionality of the Gallery Shortcode for displaying WordPress images on a post.

Compatibility

WordPress
since 5.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

$attrarray
Attributes of the gallery shortcode.
  • $orderstringdefault: 'ASC'. Accepts 'ASC', 'DESC'

    Order of the images in the gallery.
  • $orderbystringdefault: 'menu_order ID'. Accepts any valid SQL ORDERBY statement

    The field to use when ordering the images.
  • $idint

    Post ID.
  • $itemtagstringdefault: 'dl', or 'figure' when the theme registers HTML5 gallery support

    HTML tag to use for each image in the gallery.
  • $icontagstringdefault: 'dt', or 'div' when the theme registers HTML5 gallery support

    HTML tag to use for each image's icon.
  • $captiontagstringdefault: 'dd', or 'figcaption' when the theme registers HTML5 gallery support

    HTML tag to use for each image's caption.
  • $columnsintdefault: 3

    Number of columns of images to display.
  • $sizestring|int[]default: 'thumbnail'

    Size of the images to display. Accepts any registered image size name, or an array of width and height values in pixels (in that order).
  • $idsstringdefault: empty

    A comma-separated list of IDs of attachments to display.
  • $includestringdefault: empty

    A comma-separated list of IDs of attachments to include.
  • $excludestringdefault: empty

    A comma-separated list of IDs of attachments to exclude.
  • $linkstringdefault: empty (links to the attachment page). Accepts 'file', 'none'

    What to link each image to.

Return value

string
HTML content to display gallery.

Performance profile

How much work a call to gallery_shortcode() 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
149–187

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

Plugin surface
3 hooks

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

Called by
1

1 place 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

Further down the call graph this can also reach cache, 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 · 4 distinct outcomes

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

WhenInstructionsCalls it makes
empty($attr) && empty($output) && empty($atts) && empty($post_parent_id) && !empty($attachments) && !is_feed() && isset($valid_tags[$itemtag]) && isset($valid_tags[$captiontag]) && !$columns && !apply_filters()149–159get_post(), apply_filters(), current_theme_supports(), ASC(), post_parent(), is_feed(), tag_escape(), tag_escape(), tag_escape(), wp_kses_allowed_html(), is_rtl(), apply_filters(), sanitize_html_class(), apply_filters()
empty($attr) && empty($output) && empty($atts) && empty($post_parent_id) && !empty($attachments) && !is_feed() && isset($valid_tags[$itemtag]) && isset($valid_tags[$captiontag]) && isset($valid_tags[$icontag]) && $columns && !apply_filters()153–163get_post(), apply_filters(), current_theme_supports(), ASC(), post_parent(), is_feed(), tag_escape(), tag_escape(), tag_escape(), wp_kses_allowed_html(), floor(), is_rtl(), apply_filters(), sanitize_html_class(), apply_filters()
empty($attr) && empty($output) && empty($atts) && empty($post_parent_id) && !empty($attachments) && !is_feed() && isset($valid_tags[$itemtag]) && isset($valid_tags[$captiontag]) && isset($valid_tags[$icontag]) && !$columns && apply_filters()172–183get_post(), apply_filters(), current_theme_supports(), ASC(), post_parent(), is_feed(), tag_escape(), tag_escape(), tag_escape(), wp_kses_allowed_html(), is_rtl(), apply_filters(), current_theme_supports(), sanitize_html_class(), apply_filters()
empty($attr) && empty($output) && empty($atts) && empty($post_parent_id) && !empty($attachments) && !is_feed() && isset($valid_tags[$itemtag]) && isset($valid_tags[$captiontag]) && isset($valid_tags[$icontag]) && $columns && apply_filters()176–187get_post(), apply_filters(), current_theme_supports(), ASC(), post_parent(), is_feed(), tag_escape(), tag_escape(), tag_escape(), wp_kses_allowed_html(), floor(), is_rtl(), apply_filters(), current_theme_supports(), sanitize_html_class(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev433149–18747
8.5433149–18747
8.4433149–187478 fewer instructions than PHP 8.3
8.3441149–19147
8.2441149–19147
8.1441149–191474 fewer instructions than PHP 7.4
7.4445150–19247

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 · 3

3 hooks fire while gallery_shortcode() runs, in this order:

  1. apply_filters( post_gallery )filterline 2714 (+29 into the body)

    Filters the default gallery shortcode output.

  2. apply_filters( use_default_gallery_style )filterline 2847 (+162 into the body)

    Filters whether to print default gallery styles.

  3. apply_filters( gallery_style )filterline 2882 (+197 into the body)

    Filters the default gallery shortcode CSS styles.

Uses · 18

Show all 18

Used by · 1

Source code

function gallery_shortcode( $attr ) {	$post = get_post(); 	static $instance = 0;	++$instance; 	if ( ! empty( $attr['ids'] ) ) {		// 'ids' is explicitly ordered, unless you specify otherwise.		if ( empty( $attr['orderby'] ) ) {			$attr['orderby'] = 'post__in';		}		$attr['include'] = $attr['ids'];	} 	/**	 * Filters the default gallery shortcode output.	 *	 * If the filtered output isn't empty, it will be used instead of generating	 * the default gallery template.	 *	 * @since 2.5.0	 * @since 4.2.0 The `$instance` parameter was added.	 *	 * @see gallery_shortcode()	 *	 * @param string $output   The gallery output. Default empty.	 * @param array  $attr     Attributes of the gallery shortcode.	 * @param int    $instance Unique numeric ID of this gallery shortcode instance.	 */	$output = apply_filters( 'post_gallery', '', $attr, $instance ); 	if ( ! empty( $output ) ) {		return $output;	} 	$html5 = current_theme_supports( 'html5', 'gallery' );	$atts  = shortcode_atts(		array(			'order'      => 'ASC',			'orderby'    => 'menu_order ID',			'id'         => $post ? $post->ID : 0,			'itemtag'    => $html5 ? 'figure' : 'dl',			'icontag'    => $html5 ? 'div' : 'dt',			'captiontag' => $html5 ? 'figcaption' : 'dd',			'columns'    => 3,			'size'       => 'thumbnail',			'include'    => '',			'exclude'    => '',			'link'       => '',		),		$attr,		'gallery'	); 	$id = (int) $atts['id']; 	if ( ! empty( $atts['include'] ) ) {		$_attachments = get_posts(			array(				'include'        => $atts['include'],				'post_status'    => 'inherit',				'post_type'      => 'attachment',				'post_mime_type' => 'image',				'order'          => $atts['order'],				'orderby'        => $atts['orderby'],			)		); 		$attachments = array();		foreach ( $_attachments as $key => $val ) {			$attachments[ $val->ID ] = $_attachments[ $key ];		}	} elseif ( ! empty( $atts['exclude'] ) ) {		$post_parent_id = $id;		$attachments    = get_children(			array(				'post_parent'    => $id,				'exclude'        => $atts['exclude'],				'post_status'    => 'inherit',				'post_type'      => 'attachment',

Changelog

Introduced in 3.7.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.

5.6.0
Replaced order-style PHP type conversion functions with typecasts. Fix logic for an array of image dimensions.from the docblock
5.5.0
Ensured that galleries can be output as a list of links in feeds.from the docblock
5.3.0
Saved progress of intermediate image creation after upload.from the docblock
5.1.0
Code cleanup for WPCS 1.0.0 coding standards.from the docblock
4.6.0
Standardized filter docs to match documentation standards for PHP.from the docblock
4.2.0
Passed the shortcode instance ID to post_gallery and post_playlist filters.from the docblock
4.1.0
Added attribute to wp_get_attachment_link() to output aria-describedby.from the docblock
4.0.0
Removed use of extract().from the docblock
3.9.0
html5 gallery support, accepting 'itemtag', 'icontag', and 'captiontag' attributes.from the docblock
3.7.0
Introduced the link attribute.from the docblock
3.6.0
Added validation for tags used in gallery shortcode. Add orientation information to items.from the docblock
3.5.0
Use get_post() instead of global $post. Handle mapping of ids to include and orderby.from the docblock
2.9.0
Added support for include and exclude to shortcode.from the docblock
2.8.0
Added the $attr parameter to set the shortcode output. New attributes included such as size, itemtag, icontag, captiontag, and columns. Changed markup from div tags to dl, dt and dd tags. Support more than one gallery on the same page.from the docblock
2.5.0
Introduced.from the docblock

About this page

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