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.

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' );

Most Popular Articles

  1. What’s new in WordPress 5.6? New Features and Changes
  2. How to Add width/height to Images in WordPress
  3. What’s New in WordPress 5.5.3? Fixed 1 Bug
  4. WordPress 5.6 jQuery version updates and solution for jQuery
  5. WordPress Version List (Release Date/Code Name)
  6. What’s new in WordPress 5.5? New Features and Changes
  7. What’s New in WordPress 5.5.2? Fixed 10 security issues
  8. How to check if the current user is Administrator in WordPress
  9. Vulnerability of File Manager WordPress plugin and Solutions
  10. Recommend Premium Themes for WordPress (By Usage)

New Articles

go to top