Skip to content

Instantly share code, notes, and snippets.

@nicmare
Last active October 7, 2025 13:19
Show Gist options
  • Save nicmare/516b1c9c594598763047f37a6d316355 to your computer and use it in GitHub Desktop.
Save nicmare/516b1c9c594598763047f37a6d316355 to your computer and use it in GitHub Desktop.
Autoplay YT Video in Blocksy Popup
<?php
// attention! place your yt-video in html-block inside a blocksy popup like this:
// <iframe src="https://www.youtube.com/embed/LW0OVY9e5r0?enablejsapi=1&autoplay=1" class="yt-video" style="aspect-ratio:16/9" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
// this approach is also compatible with script blocker plugin "embed privacy" AND "complianz cookie consent". last one is mandatory!
function autoplay_yt_video_in_blocksy_popup(){
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
ctEvents.on('blocksy:micro-popups:open', (popupId) => {
let video = document.querySelector("#post-"+popupId+" .yt-video");
if(video){
video.contentWindow.postMessage( JSON.stringify({
'event': 'command',
'func': 'playVideo',
'args': []
}
), '*');
}
function getCookie(name) {
return document.cookie
.split("; ")
.find(row => row.startsWith(name + "="))
?.split("=")[1] || null;
}
function decodeHTML(str) {
let txt = document.createElement("textarea");
txt.innerHTML = str;
return txt.value;
}
function loadEmbed(popupId) {
let blocked = document.querySelector("#post-" + popupId + " .embed-privacy-container");
if (!blocked) return;
let embed_id = "_" + blocked.getAttribute("data-embed-id");
let embed_obj = window[embed_id];
try {
if (embed_obj) {
let data = typeof embed_obj === "string" ? JSON.parse(embed_obj) : embed_obj;
if (data && data.embed) {
blocked.innerHTML = decodeHTML(data.embed);
}
}
} catch (e) {
console.error("Error while inserting the embed:", e);
}
}
if (getCookie("cmplz_marketing") === "allow") {
loadEmbed(popupId);
} else {
console.log("Block the embed.");
}
document.addEventListener("cmplz_event_marketing", () => {
console.log("Load embed.");
loadEmbed(popupId);
});
});
});
</script>
<?php
}
add_action("wp_footer","autoplay_yt_video_in_blocksy_popup");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment