image_add_caption( string $html, int $id, string $caption, string $title, string $align, string $url, string $size, string $alt = '' ): string
- Since
- 2.6.0
- Source
wp-admin/includes/media.php:188
Compatibility
- WordPress
- since 2.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
$htmlstring- The image HTML markup to send.
$idint- Image attachment ID.
$captionstring- Image caption.
$titlestring- Image title attribute (not used).
$alignstring- Image CSS alignment property.
$urlstring- Image source URL (not used).
$sizestring- Image size (not used).
$altstringoptional- Image
altattribute (not used).Default:''
Return value
string- The image HTML markup with caption shortcode.
Performance profile
How much work a call to image_add_caption() 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–68
- Plugin surface
- 3 hooks
- Called by
- 0
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 71.
Third-party callbacks on 'image_add_caption_text', 'disable_captions', 'image_add_caption_shortcode' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - regexregular expression over the whole input
preg_replace_callback()called directly
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 image_add_caption() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($caption) | 17 | apply_filters() |
!empty($caption) && apply_filters() | 22 | apply_filters(), apply_filters() |
!empty($caption) && !apply_filters() && !preg_match() | 33–34 | apply_filters(), apply_filters(), preg_match() |
!empty($caption) && !apply_filters() && preg_match() | 66–68 | apply_filters(), apply_filters(), preg_match(), preg_replace_callback(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 71 | 17–68 | 5 | |
| 8.5 | 71 | 17–68 | 5 | |
| 8.4 | 71 | 17–68 | 5 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 80 | 17–77 | 5 | |
| 8.2 | 80 | 17–77 | 5 | |
| 8.1 | 80 | 17–77 | 5 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 81 | 17–78 | 5 |
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 image_add_caption() runs, in this order:
- apply_filters( disable_captions )filterline 216 (+28 into the body)
Filters whether to disable captions.
- apply_filters( image_add_caption_shortcode )filterline 249 (+61 into the body)
Filters the image HTML markup including the caption shortcode.
Uses · 1
- apply_filters()Calls the callback functions that have been added to a filter hook.
Source code
function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) { /** * Filters the caption text. * * Note: If the caption text is empty, the caption shortcode will not be appended * to the image HTML when inserted into the editor. * * Passing an empty value also prevents the {@see 'image_add_caption_shortcode'} * Filters from being evaluated at the end of image_add_caption(). * * @since 4.1.0 * * @param string $caption The original caption text. * @param int $id The attachment ID. */ $caption = apply_filters( 'image_add_caption_text', $caption, $id ); /** * Filters whether to disable captions. * * Prevents image captions from being appended to image HTML when inserted into the editor. * * @since 2.6.0 * * @param bool $bool Whether to disable appending captions. Returning true from the filter * will disable captions. Default empty string. */ if ( empty( $caption ) || apply_filters( 'disable_captions', '' ) ) { return $html; } $id = ( 0 < (int) $id ) ? 'attachment_' . $id : ''; if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) ) { return $html; } $width = $matches[1]; $caption = str_replace( array( "\r\n", "\r" ), "\n", $caption ); $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption ); // Convert any remaining line breaks to <br />. $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption ); $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html ); if ( empty( $align ) ) { $align = 'none'; } $shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]'; /** * Filters the image HTML markup including the caption shortcode. * * @since 2.6.0 * * @param string $shcode The image HTML markup with caption shortcode. * @param string $html The image HTML markup. */ return apply_filters( 'image_add_caption_shortcode', $shcode, $html );}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.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-admin/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.