wppaste
WordPress

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
Adds image shortcode with caption to editor.

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 alt attribute (not used).Default: ''

Return

string
The image HTML markup with caption shortcode.

Hooks fired · 3

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

  1. apply_filters( image_add_caption_text )filterline 204 (+16 into the body)

    Filters the caption text.

  2. apply_filters( disable_captions )filterline 216 (+28 into the body)

    Filters whether to disable captions.

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

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 );}

History

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.

About this page

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