wp_iframe_tag_add_loading_attr() WP 5.7.0
Adds loading attribute to an iframe HTML tag.
Hooks from the function
Return
String
. Converted iframe tag with loading attribute added.
Usage
wp_iframe_tag_add_loading_attr( $iframe, $context );
- $iframe(string) (required)
- The HTML iframe tag where the attribute should be added.
- $context(string) (required)
- Additional context to pass to the filters.
Changelog
Since 5.7.0 | Introduced. |
Code of wp_iframe_tag_add_loading_attr() wp iframe tag add loading attr WP 5.7
function wp_iframe_tag_add_loading_attr( $iframe, $context ) {
/**
* Filters the `loading` attribute value to add to an iframe. Default `lazy`.
*
* Returning `false` or an empty string will not add the attribute.
* Returning `true` will add the default value.
*
* @since 5.7.0
*
* @param string|bool $value The `loading` attribute value. Returning a falsey value will result in
* the attribute being omitted for the iframe. Default 'lazy'.
* @param string $iframe The HTML `iframe` tag to be filtered.
* @param string $context Additional context about how the function was called or where the iframe tag is.
*/
$value = apply_filters( 'wp_iframe_tag_add_loading_attr', 'lazy', $iframe, $context );
if ( $value ) {
if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
$value = 'lazy';
}
// Iframes should have source and dimension attributes for the `loading` attribute to be added.
if ( false === strpos( $iframe, ' src="' ) || false === strpos( $iframe, ' width="' ) || false === strpos( $iframe, ' height="' ) ) {
return $iframe;
}
return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe );
}
return $iframe;
}