How to disable lazy loading of iframe in WordPress
Last updated:
Learn how to disable lazy loading of iframe in WordPress. I'll show you how to disable lazy loading of iframe in WordPress, with source code to disable it for specific iframes.
What is iframe lazy loading?
In WordPress, lazy loading of iframe has been enabled by default since WordPress 5.7.
The loading=”lazy” will be automatically added to the iframe tag.
WordPress 5.7 What’s New? New Features and Changes
Disable iframe lazy loading
Add the following code to functions.php in your theme.
Disable iframe only
If you want to disable lazy loading of all iframe in your content by default, use the following filter.
function disable_post_content_iframe_lazy_loading( $default, $tag_name, $context ) {
if ( 'iframe' === $tag_name && 'the_content' === $context ) {
return false;
}
return $default;
}
add_filter(
'wp_lazy_loading_enabled',
'disable_post_content_iframe_lazy_loading',
10,
3
);
Disable only specific iframe
If you want to disable lazy loading for a specific iframe by default, use the following filter.
The following code is an example of disabling lazy loading for YouTube only. Please change “youtube.com” in the code accordingly.
function skip_loading_lazy_youtube_iframes( $value, $iframe, $context ) {
if ( 'the_content' === $context && false !== strpos( $iframe, 'youtube.com' ) ) {
return false;
}
return $value;
}
add_filter(
'wp_iframe_tag_add_loading_attr',
'skip_loading_lazy_youtube_iframes',
10,
3
);
Disable image and iframe
If you want to disable lazy loading by default for both image and iframe, use the following filter.
add_filter( 'wp_lazy_loading_enabled', '__return_false' );