img_caption_shortcode( array $attr, string $content = '' ): string
- Since
- 2.6.0, 3.9.0, 5.1.0, 5.9.0
- Source
wp-includes/media.php:2490
Description
Allows a plugin to replace the content that would otherwise be returned. The filter is 'img_caption_shortcode' and passes an empty string, the attr parameter and the content parameter values.
The supported attributes for the shortcode are 'id', 'caption_id', 'align', 'width', 'caption', and 'class'.
Compatibility
- WordPress
- since 5.9.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 caption shortcode.
$idstringID of the image and caption container element, i.e.<figure>or<div>.$caption_idstringID of the caption element, i.e.<figcaption>or<p>.$alignstringdefault: 'alignnone'. Accepts 'alignleft', 'aligncenter', alignright', 'alignnone'Class name that aligns the caption.$widthintThe width of the caption, in pixels.$captionstringThe caption text.$classstringAdditional class name(s) added to the caption container.
$contentstringoptional- Shortcode content. Default empty string.Default:
''
Return value
string- HTML content to display the caption.
Performance profile
How much work a call to img_caption_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
- Trivial
- Scaling
- Constant
- Instructions
- 17–143
- Plugin surface
- 2 hooks
- Called by
- 1
Touches nothing outside its own arguments.
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 189.
Third-party callbacks on 'img_caption_shortcode', 'img_caption_shortcode_width' 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
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query 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 · 30 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost img_caption_shortcode() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
isset($attr) && !empty($output) | 17 | apply_filters() |
!isset($attr) && !empty($output) | 20–27 | preg_match(), apply_filters() |
isset($attr) && !empty($output) | 25 | wp_kses(), apply_filters() |
isset($attr) && empty($output) | 30–32 | apply_filters(), shortcode_atts() |
!isset($attr) && empty($output) | 33–42 | preg_match(), apply_filters(), shortcode_atts() |
isset($attr) && empty($output) | 38–40 | wp_kses(), apply_filters(), shortcode_atts() |
isset($attr) && empty($output) && !empty($atts) && !$atts | 90–102 | apply_filters(), shortcode_atts(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
!isset($attr) && empty($output) && !empty($atts) && !$atts | 93–112 | preg_match(), apply_filters(), shortcode_atts(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 96–102 | apply_filters(), shortcode_atts(), sanitize_html_class(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) && !$atts | 98–110 | wp_kses(), apply_filters(), shortcode_atts(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
!isset($attr) && empty($output) && !empty($atts) | 99–112 | preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 104–110 | wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
18 further outcomes, up to 141 instructions
isset($attr) && empty($output) && !empty($atts) | 105–117 | apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 106–118 | apply_filters(), shortcode_atts(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
!isset($attr) && empty($output) && !empty($atts) | 108–127 | preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
!isset($attr) && empty($output) && !empty($atts) | 109–128 | preg_match(), apply_filters(), shortcode_atts(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 111–117 | apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), sanitize_html_class(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 112–118 | apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 113–125 | wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 114–126 | wp_kses(), apply_filters(), shortcode_atts(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
!isset($attr) && empty($output) && !empty($atts) | 114–127 | preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), sanitize_html_class(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
!isset($attr) && empty($output) && !empty($atts) | 115–128 | preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 119–125 | wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), sanitize_html_class(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 120–126 | wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 121–133 | apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
!isset($attr) && empty($output) && !empty($atts) | 124–143 | preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) && $atts | 127–133 | apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), sanitize_html_class(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) | 129–141 | wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
!isset($attr) && empty($output) && !empty($atts) && $atts | 130–143 | preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), sanitize_html_class(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
isset($attr) && empty($output) && !empty($atts) && $atts | 135–141 | wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), esc_attr(), sanitize_html_class(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 189 | 17–143 | 13 | |
| 8.5 | 189 | 17–143 | 13 | |
| 8.4 | 189 | 17–143 | 13 | 14 fewer instructions than PHP 8.3 |
| 8.3 | 203 | 20–154 | 13 | |
| 8.2 | 203 | 20–154 | 13 | |
| 8.1 | 203 | 20–154 | 13 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 206 | 20–155 | 13 |
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 · 2
2 hooks fire while img_caption_shortcode() runs, in this order:
- apply_filters( img_caption_shortcode )filterline 2515 (+25 into the body)
Filters the default caption shortcode output.
- apply_filters( img_caption_shortcode_width )filterline 2581 (+91 into the body)
Filters the width of an image's caption.
Uses · 8
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_kses()Filters text content and strips out disallowed HTML.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- shortcode_atts()Combines user attributes with known attributes and fill in defaults when needed.
- sanitize_html_class()Sanitizes an HTML classname to ensure it only contains valid characters.
- esc_attr()Escaping for HTML attributes.
- current_theme_supports()Checks a theme's support for a given feature.
- do_shortcode()Searches content for shortcodes and filter shortcodes through their hooks.
Used by · 1
- WP_Widget_Media_Image::render_media()Render the media on the frontend.
Source code
function img_caption_shortcode( $attr, $content = '' ) { // New-style shortcode with the caption inside the shortcode with the link and image tags. if ( ! isset( $attr['caption'] ) ) { if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) { $content = $matches[1]; $attr['caption'] = trim( $matches[2] ); } } elseif ( str_contains( $attr['caption'], '<' ) ) { $attr['caption'] = wp_kses( $attr['caption'], 'post' ); } /** * Filters the default caption shortcode output. * * If the filtered output isn't empty, it will be used instead of generating * the default caption template. * * @since 2.6.0 * * @see img_caption_shortcode() * * @param string $output The caption output. Default empty. * @param array $attr Attributes of the caption shortcode. * @param string $content The image element, possibly wrapped in a hyperlink. */ $output = apply_filters( 'img_caption_shortcode', '', $attr, $content ); if ( ! empty( $output ) ) { return $output; } $atts = shortcode_atts( array( 'id' => '', 'caption_id' => '', 'align' => 'alignnone', 'width' => '', 'caption' => '', 'class' => '', ), $attr, 'caption' ); $atts['width'] = (int) $atts['width']; if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) { return $content; } $id = ''; $caption_id = ''; $describedby = ''; if ( $atts['id'] ) { $atts['id'] = sanitize_html_class( $atts['id'] ); $id = 'id="' . esc_attr( $atts['id'] ) . '" '; } if ( $atts['caption_id'] ) { $atts['caption_id'] = sanitize_html_class( $atts['caption_id'] ); } elseif ( $atts['id'] ) { $atts['caption_id'] = 'caption-' . str_replace( '_', '-', $atts['id'] ); } if ( $atts['caption_id'] ) { $caption_id = 'id="' . esc_attr( $atts['caption_id'] ) . '" '; $describedby = 'aria-describedby="' . esc_attr( $atts['caption_id'] ) . '" '; } $class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] ); $html5 = current_theme_supports( 'html5', 'caption' ); // HTML5 captions never added the extra 10px to the image width. $width = $html5 ? $atts['width'] : ( 10 + $atts['width'] ); /** * Filters the width of an image's caption. * * By default, the caption is 10 pixels greater than the width of the image,Changelog
Introduced in 2.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$content parameter default value changed from null to ''.from the docblockcaption_id attribute was added.from the docblockclass attribute was added.from the docblockAbout 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.