: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @param string $filename The file path.
* @param array $image_info Optional. Extended image information (passed by reference).
* @return array|false Array of image information or false on failure.
function wp_getimagesize( $filename, ?array &$image_info = null ) {
// Don't silence errors when in debug mode, unless running unit tests.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG
&& ! defined( 'WP_RUN_CORE_TESTS' )
if ( 2 === func_num_args() ) {
$info = getimagesize( $filename, $image_info );
$info = getimagesize( $filename );
* Silencing notice and warning is intentional.
* getimagesize() has a tendency to generate errors, such as
* "corrupt JPEG data: 7191 extraneous bytes before marker",
* even when it's able to provide image size information.
* See https://core.trac.wordpress.org/ticket/42480
if ( 2 === func_num_args() ) {
$info = @getimagesize( $filename, $image_info );
$info = @getimagesize( $filename );
// Some PHP versions return 0x0 sizes from `getimagesize` for unrecognized image formats, including AVIFs.
! ( empty( $info[0] ) && empty( $info[1] ) )
* For PHP versions that don't support WebP images,
* extract the image size info from the file headers.
if ( 'image/webp' === wp_get_image_mime( $filename ) ) {
$webp_info = wp_get_webp_info( $filename );
$width = $webp_info['width'];
$height = $webp_info['height'];
// Mimic the native return format.
if ( $width && $height ) {
'width="%d" height="%d"',
// For PHP versions that don't support AVIF images, extract the image size info from the file headers.
if ( 'image/avif' === wp_get_image_mime( $filename ) ) {
$avif_info = wp_get_avif_info( $filename );
$width = $avif_info['width'];
$height = $avif_info['height'];
// Mimic the native return format.
if ( $width && $height ) {
'width="%d" height="%d"',
// The image could not be parsed.
* Extracts meta information about an AVIF file: width, height, bit depth, and number of channels.
* @param string $filename Path to an AVIF file.
* An array of AVIF image information.
* @type int|false $width Image width on success, false on failure.
* @type int|false $height Image height on success, false on failure.
* @type int|false $bit_depth Image bit depth on success, false on failure.
* @type int|false $num_channels Image number of channels on success, false on failure.
function wp_get_avif_info( $filename ) {
if ( 'image/avif' !== wp_get_image_mime( $filename ) ) {
// Parse the file using libavifinfo's PHP implementation.
require_once ABSPATH . WPINC . '/class-avif-info.php';
$handle = fopen( $filename, 'rb' );
$parser = new Avifinfo\Parser( $handle );
$success = $parser->parse_ftyp() && $parser->parse_file();
$results = $parser->features->primary_item_features;
* Extracts meta information about a WebP file: width, height, and type.
* @param string $filename Path to a WebP file.
* An array of WebP image information.
* @type int|false $width Image width on success, false on failure.
* @type int|false $height Image height on success, false on failure.
* @type string|false $type The WebP type: one of 'lossy', 'lossless' or 'animated-alpha'.
function wp_get_webp_info( $filename ) {
if ( 'image/webp' !== wp_get_image_mime( $filename ) ) {
return compact( 'width', 'height', 'type' );
$magic = file_get_contents( $filename, false, null, 0, 40 );
if ( false === $magic ) {
return compact( 'width', 'height', 'type' );
// Make sure we got enough bytes.
if ( strlen( $magic ) < 40 ) {
return compact( 'width', 'height', 'type' );
* The headers are a little different for each of the three formats.
* Header values based on WebP docs, see https://developers.google.com/speed/webp/docs/riff_container.
switch ( substr( $magic, 12, 4 ) ) {
$parts = unpack( 'v2', substr( $magic, 26, 4 ) );
$width = (int) ( $parts[1] & 0x3FFF );
$height = (int) ( $parts[2] & 0x3FFF );
$parts = unpack( 'C4', substr( $magic, 21, 4 ) );
$width = (int) ( $parts[1] | ( ( $parts[2] & 0x3F ) << 8 ) ) + 1;
$height = (int) ( ( ( $parts[2] & 0xC0 ) >> 6 ) | ( $parts[3] << 2 ) | ( ( $parts[4] & 0x03 ) << 10 ) ) + 1;
$width = unpack( 'V', substr( $magic, 24, 3 ) . "\x00" );
$width = (int) ( $width[1] & 0xFFFFFF ) + 1;
$height = unpack( 'V', substr( $magic, 27, 3 ) . "\x00" );
$height = (int) ( $height[1] & 0xFFFFFF ) + 1;
$type = 'animated-alpha';
return compact( 'width', 'height', 'type' );
* Gets loading optimization attributes.
* This function returns an array of attributes that should be merged into the given attributes array to optimize
* loading performance. Potential attributes returned by this function are:
* - `loading` attribute with a value of "lazy"
* - `fetchpriority` attribute with a value of "high"
* - `decoding` attribute with a value of "async"
* If any of these attributes are already present in the given attributes, they will not be modified. Note that no
* element should have both `loading="lazy"` and `fetchpriority="high"`, so the function will trigger a warning in case
* both attributes are present with those values.
* @global WP_Query $wp_query WordPress Query object.
* @param string $tag_name The tag name.
* @param array $attr Array of the attributes for the tag.
* @param string $context Context for the element for which the loading optimization attribute is requested.
* @return array Loading optimization attributes.
function wp_get_loading_optimization_attributes( $tag_name, $attr, $context ) {
* Filters whether to short-circuit loading optimization attributes.
* Returning an array from the filter will effectively short-circuit the loading of optimization attributes,
* returning that value instead.
* @param array|false $loading_attrs False by default, or array of loading optimization attributes to short-circuit.
* @param string $tag_name The tag name.
* @param array $attr Array of the attributes for the tag.
* @param string $context Context for the element for which the loading optimization attribute is requested.
$loading_attrs = apply_filters( 'pre_wp_get_loading_optimization_attributes', false, $tag_name, $attr, $context );
if ( is_array( $loading_attrs ) ) {
$loading_attrs = array();
* Skip lazy-loading for the overall block template, as it is handled more granularly.
* The skip is also applicable for `fetchpriority`.
if ( 'template' === $context ) {
/** This filter is documented in wp-includes/media.php */
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
// For now this function only supports images and iframes.
if ( 'img' !== $tag_name && 'iframe' !== $tag_name ) {
/** This filter is documented in wp-includes/media.php */
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
* Skip programmatically created images within content blobs as they need to be handled together with the other
* images within the post content or widget content.
* Without this clause, they would already be considered within their own context which skews the image count and
* can result in the first post content image being lazy-loaded or an image further down the page being marked as a
'the_content' !== $context && doing_filter( 'the_content' ) ||
'widget_text_content' !== $context && doing_filter( 'widget_text_content' ) ||
'widget_block_content' !== $context && doing_filter( 'widget_block_content' )
/** This filter is documented in wp-includes/media.php */
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
* Add `decoding` with a value of "async" for every image unless it has a
* conflicting `decoding` attribute already present.
if ( 'img' === $tag_name ) {
if ( isset( $attr['decoding'] ) ) {
$loading_attrs['decoding'] = $attr['decoding'];
$loading_attrs['decoding'] = 'async';
// For any resources, width and height must be provided, to avoid layout shifts.
if ( ! isset( $attr['width'], $attr['height'] ) ) {
/** This filter is documented in wp-includes/media.php */
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
* The key function logic starts here.
$maybe_in_viewport = null;
$maybe_increase_count = false;
// Logic to handle a `loading` attribute that is already provided.
if ( isset( $attr['loading'] ) ) {
* Interpret "lazy" as not in viewport. Any other value can be
* interpreted as in viewport (realistically only "eager" or `false`
* to force-omit the attribute are other potential values).
if ( 'lazy' === $attr['loading'] ) {
$maybe_in_viewport = false;
$maybe_in_viewport = true;
// Logic to handle a `fetchpriority` attribute that is already provided.
if ( isset( $attr['fetchpriority'] ) && 'high' === $attr['fetchpriority'] ) {
* If the image was already determined to not be in the viewport (e.g.
* from an already provided `loading` attribute), trigger a warning.
* Otherwise, the value can be interpreted as in viewport, since only
* the most important in-viewport image should have `fetchpriority` set
if ( false === $maybe_in_viewport ) {
__( 'An image should not be lazy-loaded and marked as high priority at the same time.' ),
* Set `fetchpriority` here for backward-compatibility as we should
* not override what a developer decided, even though it seems
$loading_attrs['fetchpriority'] = 'high';
$maybe_in_viewport = true;
if ( null === $maybe_in_viewport ) {
$header_enforced_contexts = array(
'template_part_' . WP_TEMPLATE_PART_AREA_HEADER => true,
'get_header_image_tag' => true,
* Filters the header-specific contexts.
* @param array $default_header_enforced_contexts Map of contexts for which elements should be considered
* in the header of the page, as $context => $enabled
* pairs. The $enabled should always be true.
$header_enforced_contexts = apply_filters( 'wp_loading_optimization_force_header_contexts', $header_enforced_contexts );
// Consider elements with these header-specific contexts to be in viewport.
if ( isset( $header_enforced_contexts[ $context ] ) ) {
$maybe_in_viewport = true;
$maybe_increase_count = true;
} elseif ( ! is_admin() && in_the_loop() && is_main_query() ) {
* Get the content media count, since this is a main query
* content element. This is accomplished by "increasing"
* the count by zero, as the only way to get the count is
* The actual count increase happens further below, based
* on the `$increase_count` flag set here.
$content_media_count = wp_increase_content_media_count( 0 );
// If the count so far is below the threshold, `loading` attribute is omitted.
if ( $content_media_count < wp_omit_loading_attr_threshold() ) {
$maybe_in_viewport = true;
$maybe_in_viewport = false;
// Only apply for main query but before the loop.
$wp_query->before_loop && $wp_query->is_main_query()
* Any image before the loop, but after the header has started should not be lazy-loaded,
* except when the footer has already started which can happen when the current template
* does not include any loop.
&& did_action( 'get_header' ) && ! did_action( 'get_footer' )
$maybe_in_viewport = true;
$maybe_increase_count = true;
* If the element is in the viewport (`true`), potentially add
* `fetchpriority` with a value of "high". Otherwise, i.e. if the element
* is not not in the viewport (`false`) or it is unknown (`null`), add
* `loading` with a value of "lazy".
if ( $maybe_in_viewport ) {
$loading_attrs = wp_maybe_add_fetchpriority_high_attr( $loading_attrs, $tag_name, $attr );
// Only add `loading="lazy"` if the feature is enabled.
if ( wp_lazy_loading_enabled( $tag_name, $context ) ) {
$loading_attrs['loading'] = 'lazy';
* If flag was set based on contextual logic above, increase the content
* media count, either unconditionally, or based on whether the image size
* is larger than the threshold.
wp_increase_content_media_count();
} elseif ( $maybe_increase_count ) {
/** This filter is documented in wp-includes/media.php */
$wp_min_priority_img_pixels = apply_filters( 'wp_min_priority_img_pixels', 50000 );
if ( $wp_min_priority_img_pixels <= $attr['width'] * $attr['height'] ) {
wp_increase_content_media_count();
* Filters the loading optimization attributes.
* @param array $loading_attrs The loading optimization attributes.
* @param string $tag_name The tag name.
* @param array $attr Array of the attributes for the tag.
* @param string $context Context for the element for which the loading optimization attribute is requested.
return apply_filters( 'wp_get_loading_optimization_attributes', $loading_attrs, $tag_name, $attr, $context );
* Gets the threshold for how many of the first content media elements to not lazy-load.
* This function runs the {@see 'wp_omit_loading_attr_threshold'} filter, which uses a default threshold value of 3.
* The filter is only run once per page load, unless the `$force` parameter is used.
* @param bool $force Optional. If set to true, the filter will be (re-)applied even if it already has been before.
* @return int The number of content media elements to not lazy-load.
function wp_omit_loading_attr_threshold( $force = false ) {
// This function may be called multiple times. Run the filter only once per page load.
if ( ! isset( $omit_threshold ) || $force ) {
* Filters the threshold for how many of the first content media elements to not lazy-load.
* For these first content media elements, the `loading` attribute will be omitted. By default, this is the case
* for only the very first content media element.
* @since 6.3.0 The default threshold was changed from 1 to 3.
* @param int $omit_threshold The number of media elements where the `loading` attribute will not be added. Default 3.
$omit_threshold = apply_filters( 'wp_omit_loading_attr_threshold', 3 );
* Increases an internal content media count variable.
* @param int $amount Optional. Amount to increase by. Default 1.
* @return int The latest content media count, after the increase.
function wp_increase_content_media_count( $amount = 1 ) {
static $content_media_count = 0;
$content_media_count += $amount;
return $content_media_count;
* Determines whether to add `fetchpriority='high'` to loading attributes.