Adds loading
attribute to an iframe
HTML tag.
Parameters
$iframe
stringrequired- The HTML
iframe
tag where the attribute should be added. $context
stringrequired- Additional context to pass to the filters.
Source
// Validate the values after filtering.
if ( isset( $optimization_attrs['loading'] ) && ! $filtered_loading_attr ) {
// Unset `loading` attributes if `$filtered_loading_attr` is set to `false`.
unset( $optimization_attrs['loading'] );
} elseif ( in_array( $filtered_loading_attr, array( 'lazy', 'eager' ), true ) ) {
/*
* If the filter changed the loading attribute to "lazy" when a fetchpriority attribute
* with value "high" is already present, trigger a warning since those two attribute
* values should be mutually exclusive.
*
* The same warning is present in `wp_get_loading_optimization_attributes()`, and here it
* is only intended for the specific scenario where the above filtered caused the problem.
*/
if ( isset( $optimization_attrs['fetchpriority'] ) && 'high' === $optimization_attrs['fetchpriority'] &&
( isset( $optimization_attrs['loading'] ) ? $optimization_attrs['loading'] : false ) !== $filtered_loading_attr &&
'lazy' === $filtered_loading_attr
) {
_doing_it_wrong(
__FUNCTION__,
__( 'An image should not be lazy-loaded and marked as high priority at the same time.' ),
'6.3.0'
);
}
// The filtered value will still be respected.
$optimization_attrs['loading'] = $filtered_loading_attr;
}
if ( ! empty( $optimization_attrs['loading'] ) ) {
$image = str_replace( '<img', '<img loading="' . esc_attr( $optimization_attrs['loading'] ) . '"', $image );
}
}
if ( empty( $fetchpriority_val ) && ! empty( $optimization_attrs['fetchpriority'] ) ) {
$image = str_replace( '<img', '<img fetchpriority="' . esc_attr( $optimization_attrs['fetchpriority'] ) . '"', $image );
}
return $image;
}
/**
* Adds `width` and `height` attributes 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.
* @param int $attachment_id Image attachment ID.
* @return string Converted 'img' element with 'width' and 'height' attributes added.
*/
function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id ) {
$image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
list( $image_src ) = explode( '?', $image_src );
// Return early if we couldn't get the image source.
if ( ! $image_src ) {
return $image;
}
/**
* Filters whether to add the missing `width` and `height` HTML attributes to the img tag. Default `true`.
Changelog
Version | Description |
---|---|
5.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.