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:2711
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 statementThe field to use when ordering the images.$idintPost ID.$itemtagstringdefault: 'dl', or 'figure' when the theme registers HTML5 gallery supportHTML tag to use for each image in the gallery.$icontagstringdefault: 'dt', or 'div' when the theme registers HTML5 gallery supportHTML tag to use for each image's icon.$captiontagstringdefault: 'dd', or 'figcaption' when the theme registers HTML5 gallery supportHTML tag to use for each image's caption.$columnsintdefault: 3Number 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: emptyA comma-separated list of IDs of attachments to display.$includestringdefault: emptyA comma-separated list of IDs of attachments to include.$excludestringdefault: emptyA 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
- Scaling
- Scales with input
- Instructions
- 149–187
- Plugin surface
- 3 hooks
- Called by
- 1
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 433.
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.
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()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.
| When | Instructions | Calls 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–159 | get_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–163 | get_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–183 | get_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–187 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 433 | 149–187 | 47 | |
| 8.5 | 433 | 149–187 | 47 | |
| 8.4 | 433 | 149–187 | 47 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 441 | 149–191 | 47 | |
| 8.2 | 441 | 149–191 | 47 | |
| 8.1 | 441 | 149–191 | 47 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 445 | 150–192 | 47 |
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:
- apply_filters( post_gallery )filterline 2740 (+29 into the body)
Filters the default gallery shortcode output.
- apply_filters( use_default_gallery_style )filterline 2873 (+162 into the body)
Filters whether to print default gallery styles.
- apply_filters( gallery_style )filterline 2908 (+197 into the body)
Filters the default gallery shortcode CSS styles.
Uses · 18
- get_post()Retrieves post data given a post ID or post object.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- current_theme_supports()Checks a theme's support for a given feature.
- shortcode_atts()Combines user attributes with known attributes and fill in defaults when needed.
- get_posts()Retrieves an array of the latest posts, or posts matching the given criteria.
- get_children()Retrieves all children of the post parent ID.
- is_post_publicly_viewable()Determines whether a post is publicly viewable.
- current_user_can()Returns whether the current user has the specified capability.
- post_password_required()Determines whether the post requires password and whether a correct password has been provided.
- is_feed()Determines whether the query is for a feed.
- wp_get_attachment_image()Gets an HTML img element representing an image attachment.
- wp_get_attachment_link()Retrieves an attachment page link using an image or icon, if possible.
Show all 18
- tag_escape()Escapes an HTML tag name.
- wp_kses_allowed_html()Returns an array of allowed HTML tags and attributes for a given context.
- is_rtl()Determines whether the current locale is right-to-left (RTL).
- sanitize_html_class()Sanitizes an HTML classname to ensure it only contains valid characters.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- wptexturize()Replaces common plain text characters with formatted entities.
Used by · 1
- WP_Widget_Media_Gallery::render_media()Render the media on the frontend.
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.
Signature, return type and hooks compared across 5 parsed releases.
post_gallery and post_playlist filters.from the docblockwp_get_attachment_link() to output aria-describedby.from the docblockextract().from the docblockhtml5 gallery support, accepting 'itemtag', 'icontag', and 'captiontag' attributes.from the docblocklink attribute.from the docblock$post. Handle mapping of ids to include and orderby.from the docblockinclude and exclude to shortcode.from the docblock$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 docblockAbout 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.