Skip to content

Instantly share code, notes, and snippets.

@nameDark
Created December 18, 2019 11:07
Show Gist options
  • Save nameDark/8f18d67fed486f0f5b6e213541b5f16d to your computer and use it in GitHub Desktop.
Save nameDark/8f18d67fed486f0f5b6e213541b5f16d to your computer and use it in GitHub Desktop.
Youtube lazy
// functions.php
add_filter( 'embed_oembed_html', function ($html, $url, $attr, $post_ID) {
$youtube_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
if ( preg_match( $youtube_pattern, $url ) ) {
$html = str_replace( 'src="', 'class="lazy-youtube" src="" data-src="', $html);
}
return $html;
}, 11, 4);
//js
function load_youtube () {
$('.lazy-youtube').each(function() {
var newsrc = $(this).attr('data-src');
$(this).attr('src', newsrc );
});
}
$(window).load(function() {
setTimeout(load_youtube, 3500);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment