Created
June 1, 2022 18:13
-
-
Save yankiara/d3528968b759ab314050a3fbaf5af8fb to your computer and use it in GitHub Desktop.
Defer loading of YouTube player
This file contains hidden or 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
<?php | |
add_shortcode( 'pavenum_video', function( $atts ) { | |
$a = shortcode_atts( array( | |
'id' => '', | |
'ratio' => '', | |
/*'legend' => '',*/ | |
'alt' => '' | |
), $atts ); | |
$html = '<style>.youtube { | |
position: relative; | |
padding-top: 56.23%; | |
overflow: hidden; | |
cursor: pointer; | |
background-color: black; | |
border-radius: var(--frame-radius); | |
} | |
.youtube img { | |
width: 100%; | |
height: 100%; | |
top: 0; | |
left: 0; | |
object-fit: cover; | |
opacity: 0.5; | |
} | |
.youtube .play-button { | |
width: 90px; | |
height: 60px; | |
background-color: var(--secondary); | |
box-shadow: var(--box-shadow); | |
z-index: 1; | |
border-radius: 6px; | |
} | |
.youtube .play-button:before { | |
content: ""; | |
border-style: solid; | |
border-width: 15px 0 15px 26.0px; | |
border-color: transparent transparent transparent #fff; | |
} | |
.youtube img, | |
.youtube .play-button { | |
cursor: pointer; | |
} | |
.youtube img, | |
.youtube iframe, | |
.youtube .play-button, | |
.youtube .play-button:before { | |
position: absolute; | |
} | |
.youtube .play-button, | |
.youtube .play-button:before { | |
top: 50%; | |
left: 50%; | |
transform: translate3d( -50%, -50%, 0 ); | |
} | |
.youtube iframe { | |
height: 100%; | |
width: 100%; | |
top: 0; | |
left: 0; | |
}</style>'; | |
$html .= '<div class="wrapper"><div class="youtube" data-embed="' . $a['id'] . '" data-alttext="' . $a['alt'] . '" data-placeholder=""><div class="play-button"></div></div></div>'; | |
$html .= '<script>( function() { | |
var youtube = document.querySelectorAll( ".youtube" ); | |
for (var i = 0; i < youtube.length; i++) { | |
//var source = "https://img.youtube.com/vi/"+ youtube[i].dataset.embed +"/hqdefault.jpg"; | |
var source = "https://i.ytimg.com/vi_webp/"+ youtube[i].dataset.embed +"/maxresdefault.webp"; | |
//var source = youtube[i].dataset.placeholder; | |
var alt_text = youtube[i].dataset.alttext; | |
var image = new Image(); | |
image.src = source; | |
image.alt = alt_text; | |
image.addEventListener( "load", function() { | |
youtube[ i ].appendChild( image ); | |
}( i ) ); | |
youtube[i].addEventListener( "click", function() { | |
var iframe = document.createElement( "iframe" ); | |
iframe.setAttribute( "frameborder", "0" ); | |
iframe.setAttribute( "allowfullscreen", "" ); | |
iframe.setAttribute( "src", "https://www.youtube.com/embed/"+ this.dataset.embed +"?rel=0&showinfo=0&autoplay=1" ); | |
this.innerHTML = ""; | |
this.appendChild( iframe ); | |
} ); | |
}; | |
} )();</script>'; | |
return $html; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment