Created
December 18, 2019 11:07
-
-
Save nameDark/8f18d67fed486f0f5b6e213541b5f16d to your computer and use it in GitHub Desktop.
Youtube lazy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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