wppaste
WordPress

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:2628
Builds the Caption shortcode output.

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.
  • $idstring

    ID of the image and caption container element, i.e. <figure> or <div>.
  • $caption_idstring

    ID of the caption element, i.e. <figcaption> or <p>.
  • $alignstringdefault: 'alignnone'. Accepts 'alignleft', 'aligncenter', alignright', 'alignnone'

    Class name that aligns the caption.
  • $widthint

    The width of the caption, in pixels.
  • $captionstring

    The caption text.
  • $classstring

    Additional 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
17–155

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

Plugin surface
2 hooks

Third-party callbacks on 'img_caption_shortcode', 'img_caption_shortcode_width' 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

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
isset($attr) && !empty($output)17apply_filters()
!isset($attr) && !empty($output)20–27preg_match(), apply_filters()
isset($attr) && !empty($output)25wp_kses(), apply_filters()
isset($attr) && empty($output)30–32apply_filters(), shortcode_atts()
!isset($attr) && empty($output)33–42preg_match(), apply_filters(), shortcode_atts()
isset($attr) && empty($output)38–40wp_kses(), apply_filters(), shortcode_atts()
isset($attr) && empty($output) && !empty($atts) && !$atts90–100apply_filters(), shortcode_atts(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
!isset($attr) && empty($output) && !empty($atts) && !$atts93–110preg_match(), apply_filters(), shortcode_atts(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts) && !$atts98–108wp_kses(), apply_filters(), shortcode_atts(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts) && !$atts102–112apply_filters(), shortcode_atts(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts)105–111apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
!isset($attr) && empty($output) && !empty($atts) && !$atts105–122preg_match(), apply_filters(), shortcode_atts(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
18 further outcomes, up to 153 instructions
!isset($attr) && empty($output) && !empty($atts)108–121preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts) && !$atts110–120wp_kses(), apply_filters(), shortcode_atts(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts)112–122apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts)113–119wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
!isset($attr) && empty($output) && !empty($atts)115–132preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts)117–123apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts)120–130wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
!isset($attr) && empty($output) && !empty($atts)120–133preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts)124–134apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts)125–131wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts) && $atts127–133apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), sanitize_html_class(), wp_unique_prefixed_id(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
!isset($attr) && empty($output) && !empty($atts)127–144preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
!isset($attr) && empty($output) && !empty($atts) && $atts130–143preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), sanitize_html_class(), wp_unique_prefixed_id(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts)132–142wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts) && $atts135–141wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), sanitize_html_class(), wp_unique_prefixed_id(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts) && $atts139–145apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
!isset($attr) && empty($output) && !empty($atts) && $atts142–155preg_match(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()
isset($attr) && empty($output) && !empty($atts) && $atts147–153wp_kses(), apply_filters(), shortcode_atts(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), sanitize_html_class(), wp_unique_prefixed_id(), esc_attr(), esc_attr(), current_theme_supports(), apply_filters(), esc_attr(), do_shortcode()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev19817–15513
8.519817–15513
8.419817–1551320 fewer instructions than PHP 8.3
8.321820–16913
8.221820–16913
8.121820–169133 fewer instructions than PHP 7.4
7.422120–17013

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:

  1. apply_filters( img_caption_shortcode )filterline 2653 (+25 into the body)

    Filters the default caption shortcode output.

  2. apply_filters( img_caption_shortcode_width )filterline 2735 (+107 into the body)

    Filters the width of an image's caption.

Uses · 9

Used by · 1

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;	} 	/**	 * @var array{	 *     id: string,	 *     caption_id: string,	 *     align: string,	 *     width: string,	 *     caption: string,	 *     class: string,	 * } $atts	 */	$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      = '';	$unique_id_value  = '';	$caption_id_value = ''; 	if ( $atts['id'] ) {		$atts['id']      = sanitize_html_class( $atts['id'] );		$unique_id_value = (string) preg_replace( '/-1$/', '', wp_unique_prefixed_id( $atts['id'] . '-' ) );		$id              = 'id="' . esc_attr( $unique_id_value ) . '" ';	} 	if ( $atts['caption_id'] ) {		// User explicitly provided a caption_id - make it unique.		$atts['caption_id'] = sanitize_html_class( $atts['caption_id'] );		$caption_id_value   = preg_replace( '/-1$/', '', wp_unique_prefixed_id( $atts['caption_id'] . '-' ) );	} elseif ( $unique_id_value ) {		// Derive from the already-unique figure ID - guaranteed unique, no need for second call.		$caption_id_value = 'caption-' . str_replace( '_', '-', $unique_id_value );	}

Changelog

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

5.9.0
The $content parameter default value changed from null to ''.from the docblock
5.1.0
The caption_id attribute was added.from the docblock
3.9.0
The class attribute was added.from the docblock
2.6.0
Introduced.from the docblock

About this page

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