Lazyload for videos and iframes in WP

I try to set lazyload for videos and iframes inside functions.php
How to manage videos and iframes?
Need help.

<?php
function wp_lazy_loading_enabled( $tag_name, $context ) {
	$default = ( 'img' === $tag_name );
	return (bool) apply_filters( 'wp_lazy_loading_enabled', $default, $tag_name, $context );
}

function disable_template_image_lazy_loading( $default, $tag_name, $context ) {
    if ( 'img' === $tag_name && 'wp_get_attachment_image' === $context ) {
        return false;
    }
    return $default;
}
add_filter(
    'wp_lazy_loading_enabled',
    'disable_template_image_lazy_loading',
    10,
    3
);

function skip_loading_lazy_image_42_large( $value, $image, $context ) {
    if ( 'the_content' === $context ) {
        $image_url = wp_get_attachment_image_url( 42, 'large' );
        if ( false !== strpos( $image, ' src="' . $image_url . '"' ) {
            return false;
        }
    }
    return $value;
}
add_filter(
    'wp_img_tag_add_loading_attr',
    'skip_loading_lazy_image_42_large',
    10,
    3
);

?>
1 Like

I very much doubt there is a way to lazy load iframes.

You can see URL where is possible:
https://wp-rocket.me/blog/lazy-loading-wordpress-5-5/

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.