How to add a custom class for iframe by embed_oembed_html

You can’t just add a class because WordPress doesn’t know anything about the existing classes or have any control over them, the way it does posts and nav items. This is because the HTML comes from the 3rd party raw.

So to replace the class you need to use str_replace(). You can use it to add a class, or any text, by doing it something like this:

$cache = str_replace( '<iframe class="existing-class ', '<iframe class="existing-class lazy-load ', $cache );

But that example will only work A. If the embed code is an iframe (which isn’t always guaranteed) and B. class="" is the first attribute in the tag.

This means exact string to replace is going to differ based on the original HTML, which will be different for every provider. Inside your filter you’ll need to check the source and do a different str_replace() to accommodate all the providers you need to support with this class:

function wpse_287229_embed_html( $cache, $url, $attr, $post_ID ) {
    if ( 
        strpos( $cache, 'youtube.com' ) !== false || 
        strpos( $cache, 'youtu.be' ) !== false 
    ) {
        $cache = str_replace( '<iframe ', '<iframe class="my-class" ', $cache ); // YouTube doesn't have a class on its iframe.
    }

    if ( strpos( $cache, 'soundcloud.com' ) !== false ) {
        // str_replace optimised for SoundCloud here.
    }

    // etc.

    return $cache;
}
add_filter( 'embed_oembed_html', 'wpse_287229_embed_html', 10, 4 );