Hi, i have saved the script in my child-theme as youtube-light-player.js
Here is youtube-light-player.js
/* Light YouTube Embeds by @labnol */
/* Web: https://labnol.org/?p=27941 */
document.addEventListener("DOMContentLoaded",
function() {
var div, n,
v = document.getElementsByClassName("youtube-player");
for (n = 0; n < v.length; n++) {
div = document.createElement("div");
div.setAttribute("data-id", v[n].dataset.id);
div.innerHTML = labnolThumb(v[n].dataset.id);
div.onclick = labnolIframe;
v[n].appendChild(div);
}
});
function labnolThumb(id) {
var thumb = '<img src="https://i.ytimg.com/vi/ID/hqdefault.jpg">',
play = '<div class="play"></div>';
return thumb.replace("ID", id) + play;
}
function labnolIframe() {
var iframe = document.createElement("iframe");
var embed = "https://www.youtube.com/embed/ID?autoplay=1";
iframe.setAttribute("src", embed.replace("ID", this.dataset.id));
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("allowfullscreen", "1");
this.parentNode.replaceChild(iframe, this);
}
and included it in my single-custom-post-type.php like this below
<script src="/wp-content/themes/childhueman/my-custom-js/youtube-light-player.js" async></script>
and in my child theme functions i created this shortcode below
add_shortcode('collapso_short_contributor_video','collapso_short_contributor_video_fctn');
function collapso_short_contributor_video_fctn() {
$output = '';
$grows = CFS()->get('youtube_video_id');
$dmrows = CFS()->get('dailymotion_embed_code');
if ($grows) {
ob_start ();
$utube_video_id = CFS()->get('youtube_video_id');
$utube_username = CFS()->get('youtube_username');
echo '<div class="targ-video">';
echo '<div class="youtube-player" data-id="'.$utube_video_id.'"></div>';
echo '</div>';
$grow_content = ob_get_contents ();
ob_end_clean ();
$output .= '<div class="collapsomydiv" id="collapsomyid">';
$output .= do_shortcode( '[expand title="▼ Show Video" expanded="true" notitle="true" id="collapseoutube" trigclass="mytrigclass snap-shut" targclass="targoutube" swaptitle="▲ Hide Video" tag="h2"]' .$grow_content. '[/expand]' );
$output .= '</div>';
$output .= '<div class="clear"></div>';
}
return $output;
}
and then displaying the video by including this shortcode in my posts content
[collapso_short_contributor_video]