wp_make_content_images_responsive

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

WordPress Version: 5.5

/**
 * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
 *
 * @since 4.4.0
 * @deprecated 5.5.0
 *
 * @see wp_image_add_srcset_and_sizes()
 *
 * @param string $content The raw post content to be filtered.
 * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
 */
function wp_make_content_images_responsive($content)
{
    _deprecated_function(__FUNCTION__, '5.5.0', 'wp_filter_content_tags()');
    // This will also add the `loading` attribute to `img` tags, if enabled.
    return wp_filter_content_tags($content);
}

WordPress Version: 5.3

/**
 * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
 *
 * @since 4.4.0
 *
 * @see wp_image_add_srcset_and_sizes()
 *
 * @param string $content The raw post content to be filtered.
 * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
 */
function wp_make_content_images_responsive($content)
{
    if (!preg_match_all('/<img [^>]+>/', $content, $matches)) {
        return $content;
    }
    $selected_images = array();
    $attachment_ids = array();
    foreach ($matches[0] as $image) {
        if (false === strpos($image, ' srcset=') && preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) {
            $attachment_id = absint($class_id[1]);
            if ($attachment_id) {
                /*
                 * If exactly the same image tag is used more than once, overwrite it.
                 * All identical tags will be replaced later with 'str_replace()'.
                 */
                $selected_images[$image] = $attachment_id;
                // Overwrite the ID when the same image is included more than once.
                $attachment_ids[$attachment_id] = true;
            }
        }
    }
    if (count($attachment_ids) > 1) {
        /*
         * Warm the object cache with post and meta information for all found
         * images to avoid making individual database calls.
         */
        _prime_post_caches(array_keys($attachment_ids), false, true);
    }
    foreach ($selected_images as $image => $attachment_id) {
        $image_meta = wp_get_attachment_metadata($attachment_id);
        $content = str_replace($image, wp_image_add_srcset_and_sizes($image, $image_meta, $attachment_id), $content);
    }
    return $content;
}

WordPress Version: 4.9

/**
 * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
 *
 * @since 4.4.0
 *
 * @see wp_image_add_srcset_and_sizes()
 *
 * @param string $content The raw post content to be filtered.
 * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
 */
function wp_make_content_images_responsive($content)
{
    if (!preg_match_all('/<img [^>]+>/', $content, $matches)) {
        return $content;
    }
    $selected_images = $attachment_ids = array();
    foreach ($matches[0] as $image) {
        if (false === strpos($image, ' srcset=') && preg_match('/wp-image-([0-9]+)/i', $image, $class_id) && $attachment_id = absint($class_id[1])) {
            /*
             * If exactly the same image tag is used more than once, overwrite it.
             * All identical tags will be replaced later with 'str_replace()'.
             */
            $selected_images[$image] = $attachment_id;
            // Overwrite the ID when the same image is included more than once.
            $attachment_ids[$attachment_id] = true;
        }
    }
    if (count($attachment_ids) > 1) {
        /*
         * Warm the object cache with post and meta information for all found
         * images to avoid making individual database calls.
         */
        _prime_post_caches(array_keys($attachment_ids), false, true);
    }
    foreach ($selected_images as $image => $attachment_id) {
        $image_meta = wp_get_attachment_metadata($attachment_id);
        $content = str_replace($image, wp_image_add_srcset_and_sizes($image, $image_meta, $attachment_id), $content);
    }
    return $content;
}

WordPress Version: 4.7

/**
 * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
 *
 * @since 4.4.0
 *
 * @see wp_image_add_srcset_and_sizes()
 *
 * @param string $content The raw post content to be filtered.
 * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
 */
function wp_make_content_images_responsive($content)
{
    if (!preg_match_all('/<img [^>]+>/', $content, $matches)) {
        return $content;
    }
    $selected_images = $attachment_ids = array();
    foreach ($matches[0] as $image) {
        if (false === strpos($image, ' srcset=') && preg_match('/wp-image-([0-9]+)/i', $image, $class_id) && $attachment_id = absint($class_id[1])) {
            /*
             * If exactly the same image tag is used more than once, overwrite it.
             * All identical tags will be replaced later with 'str_replace()'.
             */
            $selected_images[$image] = $attachment_id;
            // Overwrite the ID when the same image is included more than once.
            $attachment_ids[$attachment_id] = true;
        }
    }
    if (count($attachment_ids) > 1) {
        /*
         * Warm object cache for use with 'get_post_meta()'.
         *
         * To avoid making a database call for each image, a single query
         * warms the object cache with the meta information for all images.
         */
        update_meta_cache('post', array_keys($attachment_ids));
    }
    foreach ($selected_images as $image => $attachment_id) {
        $image_meta = wp_get_attachment_metadata($attachment_id);
        $content = str_replace($image, wp_image_add_srcset_and_sizes($image, $image_meta, $attachment_id), $content);
    }
    return $content;
}

WordPress Version: 4.4

/**
 * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes.
 *
 * @since 4.4.0
 *
 * @see wp_image_add_srcset_and_sizes()
 *
 * @param string $content The raw post content to be filtered.
 * @return string Converted content with 'srcset' and 'sizes' attributes added to images.
 */
function wp_make_content_images_responsive($content)
{
    if (!preg_match_all('/<img [^>]+>/', $content, $matches)) {
        return $content;
    }
    $selected_images = $attachment_ids = array();
    foreach ($matches[0] as $image) {
        if (false === strpos($image, ' srcset=') && preg_match('/wp-image-([0-9]+)/i', $image, $class_id) && $attachment_id = absint($class_id[1])) {
            /*
             * If exactly the same image tag is used more than once, overwrite it.
             * All identical tags will be replaced later with 'str_replace()'.
             */
            $selected_images[$image] = $attachment_id;
            // Overwrite the ID when the same image is included more than once.
            $attachment_ids[$attachment_id] = true;
        }
    }
    if (count($attachment_ids) > 1) {
        /*
         * Warm object cache for use with 'get_post_meta()'.
         *
         * To avoid making a database call for each image, a single query
         * warms the object cache with the meta information for all images.
         */
        update_meta_cache('post', array_keys($attachment_ids));
    }
    foreach ($selected_images as $image => $attachment_id) {
        $image_meta = get_post_meta($attachment_id, '_wp_attachment_metadata', true);
        $content = str_replace($image, wp_image_add_srcset_and_sizes($image, $image_meta, $attachment_id), $content);
    }
    return $content;
}