image_add_caption

The timeline below displays how wordpress function image_add_caption has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.6

/**
 * Adds image shortcode with caption to editor.
 *
 * @since 2.6.0
 *
 * @param string  $html    The image HTML markup to send.
 * @param int     $id      Image attachment ID.
 * @param string  $caption Image caption.
 * @param string  $title   Image title attribute (not used).
 * @param string  $align   Image CSS alignment property.
 * @param string  $url     Image source URL (not used).
 * @param string  $size    Image size (not used).
 * @param string  $alt     Image `alt` attribute (not used).
 * @return string The image HTML markup with caption shortcode.
 */
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);
}

WordPress Version: 5.5

/**
 * Adds image shortcode with caption to editor.
 *
 * @since 2.6.0
 *
 * @param string  $html    The image HTML markup to send.
 * @param integer $id      Image attachment ID.
 * @param string  $caption Image caption.
 * @param string  $title   Image title attribute (not used).
 * @param string  $align   Image CSS alignment property.
 * @param string  $url     Image source URL (not used).
 * @param string  $size    Image size (`thumbnail`, `medium`, `large`, `full`, or added with `add_image_size()`) (not used).
 * @param string  $alt     Image `alt` attribute (not used).
 * @return string The image HTML markup with caption shortcode.
 */
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);
}

WordPress Version: 5.4

/**
 * Adds image shortcode with caption to editor
 *
 * @since 2.6.0
 *
 * @param string  $html    The image HTML markup to send.
 * @param integer $id      Image attachment ID.
 * @param string  $caption Image caption.
 * @param string  $title   Image title attribute (not used).
 * @param string  $align   Image CSS alignment property.
 * @param string  $url     Image source URL (not used).
 * @param string  $size    Image size (`thumbnail`, `medium`, `large`, `full`, or added with `add_image_size()`) (not used).
 * @param string  $alt     Image `alt` attribute (not used).
 * @return string The image HTML markup with caption shortcode.
 */
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 to 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);
}

WordPress Version: 5.1

/**
 * Adds image shortcode with caption to editor
 *
 * @since 2.6.0
 *
 * @param string  $html    The image HTML markup to send.
 * @param integer $id      Image attachment ID.
 * @param string  $caption Image caption.
 * @param string  $title   Image title attribute (not used).
 * @param string  $align   Image CSS alignment property.
 * @param string  $url     Image source URL (not used).
 * @param string  $size    Image size (`thumbnail`, `medium`, `large`, `full`, or added with `add_image_size()`) (not used).
 * @param string  $alt     Image `alt` attribute (not used).
 * @return string The image HTML markup with caption shortcode.
 */
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 to 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);
}

WordPress Version: 4.6

/**
 * Adds image shortcode with caption to editor
 *
 * @since 2.6.0
 *
 * @param string $html
 * @param integer $id
 * @param string $caption image caption
 * @param string $title image title attribute
 * @param string $align image css alignment property
 * @param string $url image src url
 * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
 * @param string $alt image alt attribute
 * @return string
 */
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 to 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);
}

WordPress Version: 4.5

/**
 * Adds image shortcode with caption to editor
 *
 * @since 2.6.0
 *
 * @param string $html
 * @param integer $id
 * @param string $caption image caption
 * @param string $title image title attribute
 * @param string $align image css alignment property
 * @param string $url image src url
 * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
 * @param string $alt image alt attribute
 * @return string
 */
function image_add_caption($html, $id, $caption, $title, $align, $url, $size, $alt = '')
{
    /**
     * Filter 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'}
     * filter from being evaluated at the end of {@see 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);
    /**
     * Filter 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 to 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]';
    /**
     * Filter 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);
}

WordPress Version: 4.1

/**
 * Adds image shortcode with caption to editor
 *
 * @since 2.6.0
 *
 * @param string $html
 * @param integer $id
 * @param string $caption image caption
 * @param string $alt image alt attribute
 * @param string $title image title attribute
 * @param string $align image css alignment property
 * @param string $url image src url
 * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
 * @return string
 */
function image_add_caption($html, $id, $caption, $title, $align, $url, $size, $alt = '')
{
    /**
     * Filter 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'}
     * filter from being evaluated at the end of {@see 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);
    /**
     * Filter 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 to 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]';
    /**
     * Filter 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);
}

WordPress Version: 4.0

/**
 * Adds image shortcode with caption to editor
 *
 * @since 2.6.0
 *
 * @param string $html
 * @param integer $id
 * @param string $caption image caption
 * @param string $alt image alt attribute
 * @param string $title image title attribute
 * @param string $align image css alignment property
 * @param string $url image src url
 * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
 * @return string
 */
function image_add_caption($html, $id, $caption, $title, $align, $url, $size, $alt = '')
{
    /**
     * Filter 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 to 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]';
    /**
     * Filter 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);
}

WordPress Version: 3.9

/**
 * Adds image shortcode with caption to editor
 *
 * @since 2.6.0
 *
 * @param string $html
 * @param integer $id
 * @param string $caption image caption
 * @param string $alt image alt attribute
 * @param string $title image title attribute
 * @param string $align image css alignment property
 * @param string $url image src url
 * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
 * @return string
 */
function image_add_caption($html, $id, $caption, $title, $align, $url, $size, $alt = '')
{
    /**
     * Filter 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 to 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]';
    /**
     * Filter 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);
}

WordPress Version: 3.7

/**
 * Adds image shortcode with caption to editor
 *
 * @since 2.6.0
 *
 * @param string $html
 * @param integer $id
 * @param string $caption image caption
 * @param string $alt image alt attribute
 * @param string $title image title attribute
 * @param string $align image css alignment property
 * @param string $url image src url
 * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
 * @return string
 */
function image_add_caption($html, $id, $caption, $title, $align, $url, $size, $alt = '')
{
    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]';
    return apply_filters('image_add_caption_shortcode', $shcode, $html);
}