Adds srcset
and sizes
attributes to an existing img
HTML tag.
Parameters
$image
stringrequired- The HTML
img
tag where the attribute should be added. $context
stringrequired- Additional context to pass to the filters.
$attachment_id
intrequired- Image attachment ID.
Source
$image = str_replace( '<img', '<img decoding="' . esc_attr( $optimization_attrs['decoding'] ) . '"', $image );
}
}
// Images should have dimension attributes for the 'loading' and 'fetchpriority' attributes to be added.
if ( ! str_contains( $image, ' width="' ) || ! str_contains( $image, ' height="' ) ) {
return $image;
}
// Retained for backward compatibility.
$loading_attrs_enabled = wp_lazy_loading_enabled( 'img', $context );
if ( empty( $loading_val ) && $loading_attrs_enabled ) {
/**
* 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
Changelog
Version | Description |
---|---|
5.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.