wp_img_tag_add_loading_attr

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

WordPress Version: 6.3

/**
 * Adds `loading` attribute to an `img` HTML tag.
 *
 * @since 5.5.0
 * @deprecated 6.3.0 Use wp_img_tag_add_loading_optimization_attrs() instead.
 * @see wp_img_tag_add_loading_optimization_attrs()
 *
 * @param string $image   The HTML `img` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `img` tag with `loading` attribute added.
 */
function wp_img_tag_add_loading_attr($image, $context)
{
    _deprecated_function(__FUNCTION__, '6.3.0', 'wp_img_tag_add_loading_optimization_attrs()');
    /*
     * Get loading attribute value to use. This must occur before the conditional check below so that even images that
     * are ineligible for being lazy-loaded are considered.
     */
    $value = wp_get_loading_attr_default($context);
    // Images should have source and dimension attributes for the `loading` attribute to be added.
    if (!str_contains($image, ' src="') || !str_contains($image, ' width="') || !str_contains($image, ' height="')) {
        return $image;
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $value = apply_filters('wp_img_tag_add_loading_attr', $value, $image, $context);
    if ($value) {
        if (!in_array($value, array('lazy', 'eager'), true)) {
            $value = 'lazy';
        }
        return str_replace('<img', '<img loading="' . esc_attr($value) . '"', $image);
    }
    return $image;
}

WordPress Version: 5.9

/**
 * Adds `loading` attribute to an `img` HTML tag.
 *
 * @since 5.5.0
 *
 * @param string $image   The HTML `img` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `img` tag with `loading` attribute added.
 */
function wp_img_tag_add_loading_attr($image, $context)
{
    // Get loading attribute value to use. This must occur before the conditional check below so that even images that
    // are ineligible for being lazy-loaded are considered.
    $value = wp_get_loading_attr_default($context);
    // Images should have source and dimension attributes for the `loading` attribute to be added.
    if (false === strpos($image, ' src="') || false === strpos($image, ' width="') || false === strpos($image, ' height="')) {
        return $image;
    }
    /**
     * Filters the `loading` attribute value to add to an image. Default `lazy`.
     *
     * Returning `false` or an empty string will not add the attribute.
     * Returning `true` will add the default value.
     *
     * @since 5.5.0
     *
     * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in
     *                             the attribute being omitted for the image.
     * @param string      $image   The HTML `img` tag to be filtered.
     * @param string      $context Additional context about how the function was called or where the img tag is.
     */
    $value = apply_filters('wp_img_tag_add_loading_attr', $value, $image, $context);
    if ($value) {
        if (!in_array($value, array('lazy', 'eager'), true)) {
            $value = 'lazy';
        }
        return str_replace('<img', '<img loading="' . esc_attr($value) . '"', $image);
    }
    return $image;
}

WordPress Version: 7.1

/**
 * Adds `loading` attribute to an `img` HTML tag.
 *
 * @since 5.5.0
 *
 * @param string $image   The HTML `img` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `img` tag with `loading` attribute added.
 */
function wp_img_tag_add_loading_attr($image, $context)
{
    // Images should have source and dimension attributes for the `loading` attribute to be added.
    if (false === strpos($image, ' src="') || false === strpos($image, ' width="') || false === strpos($image, ' height="')) {
        return $image;
    }
    /**
     * Filters the `loading` attribute value to add to an image. Default `lazy`.
     *
     * Returning `false` or an empty string will not add the attribute.
     * Returning `true` will add the default value.
     *
     * @since 5.5.0
     *
     * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in
     *                             the attribute being omitted for the image. Default 'lazy'.
     * @param string      $image   The HTML `img` tag to be filtered.
     * @param string      $context Additional context about how the function was called or where the img tag is.
     */
    $value = apply_filters('wp_img_tag_add_loading_attr', 'lazy', $image, $context);
    if ($value) {
        if (!in_array($value, array('lazy', 'eager'), true)) {
            $value = 'lazy';
        }
        return str_replace('<img', '<img loading="' . esc_attr($value) . '"', $image);
    }
    return $image;
}

WordPress Version: 5.7

/**
 * Adds `loading` attribute to an `img` HTML tag.
 *
 * @since 5.5.0
 *
 * @param string $image   The HTML `img` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `img` tag with `loading` attribute added.
 */
function wp_img_tag_add_loading_attr($image, $context)
{
    /**
     * Filters the `loading` attribute value to add to an image. Default `lazy`.
     *
     * Returning `false` or an empty string will not add the attribute.
     * Returning `true` will add the default value.
     *
     * @since 5.5.0
     *
     * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in
     *                             the attribute being omitted for the image. Default 'lazy'.
     * @param string      $image   The HTML `img` tag to be filtered.
     * @param string      $context Additional context about how the function was called or where the img tag is.
     */
    $value = apply_filters('wp_img_tag_add_loading_attr', 'lazy', $image, $context);
    if ($value) {
        if (!in_array($value, array('lazy', 'eager'), true)) {
            $value = 'lazy';
        }
        // Images should have source and dimension attributes for the `loading` attribute to be added.
        if (false === strpos($image, ' src="') || false === strpos($image, ' width="') || false === strpos($image, ' height="')) {
            return $image;
        }
        return str_replace('<img', '<img loading="' . esc_attr($value) . '"', $image);
    }
    return $image;
}

WordPress Version: 5.6

/**
 * Adds `loading` attribute to an `img` HTML tag.
 *
 * @since 5.5.0
 *
 * @param string $image   The HTML `img` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `img` tag with `loading` attribute added.
 */
function wp_img_tag_add_loading_attr($image, $context)
{
    /**
     * Filters the `loading` attribute value. Default `lazy`.
     *
     * Returning `false` or an empty string will not add the attribute.
     * Returning `true` will add the default value.
     *
     * @since 5.5.0
     *
     * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in
     *                             the attribute being omitted for the image. Default 'lazy'.
     * @param string      $image   The HTML `img` tag to be filtered.
     * @param string      $context Additional context about how the function was called or where the img tag is.
     */
    $value = apply_filters('wp_img_tag_add_loading_attr', 'lazy', $image, $context);
    if ($value) {
        if (!in_array($value, array('lazy', 'eager'), true)) {
            $value = 'lazy';
        }
        // Images should have source and dimension attributes for the `loading` attribute to be added.
        if (false === strpos($image, ' src="') || false === strpos($image, ' width="') || false === strpos($image, ' height="')) {
            return $image;
        }
        return str_replace('<img', '<img loading="' . esc_attr($value) . '"', $image);
    }
    return $image;
}

WordPress Version: 5.5

/**
 * Adds `loading` attribute to an `img` HTML tag.
 *
 * @since 5.5.0
 *
 * @param string $image   The HTML `img` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `img` tag with `loading` attribute added.
 */
function wp_img_tag_add_loading_attr($image, $context)
{
    /**
     * Filters the `loading` attribute value. Default `lazy`.
     *
     * Returning `false` or an empty string will not add the attribute.
     * Returning `true` will add the default value.
     *
     * @since 5.5.0
     *
     * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in
     *                             the attribute being omitted for the image. Default is `lazy`.
     * @param string      $image   The HTML `img` tag to be filtered.
     * @param string      $context Additional context about how the function was called or where the img tag is.
     */
    $value = apply_filters('wp_img_tag_add_loading_attr', 'lazy', $image, $context);
    if ($value) {
        if (!in_array($value, array('lazy', 'eager'), true)) {
            $value = 'lazy';
        }
        // Images should have source and dimension attributes for the `loading` attribute to be added.
        if (false === strpos($image, ' src="') || false === strpos($image, ' width="') || false === strpos($image, ' height="')) {
            return $image;
        }
        return str_replace('<img', '<img loading="' . esc_attr($value) . '"', $image);
    }
    return $image;
}