Here is a function you can use in functions.php
. The function grabs a link and listens for link clicks. I added a 900ms delay (adjustable based on your theme) to ensure the iframe is fully loaded. Once the iframe is loaded, the function removes any related
and autoplay
parameters from the iframe and re-adds them.
document.addEventListener('DOMContentLoaded', function () {
const videoLinks = document.querySelectorAll('a[rel="wp-video-lightbox"]');
videoLinks.forEach((link) => {
link.addEventListener('click', function () {
setTimeout(() => {
let iframe = document.querySelector('iframe[src*="youtube.com/embed"]');
if (iframe) {
// Get the current src and remove any existing 'rel' and 'autoplay' parameters
let src = iframe.getAttribute('src');
// Clean the src by removing existing 'rel' or 'autoplay' parameters
src = src.replace(/[?&](rel=\d|autoplay=\d)/g, '');
// Add rel=0 to disable related videos and autoplay=1 to start the video
src += (src.includes('?') ? '&rel=0&autoplay=1' : '?rel=0&autoplay=1');
iframe.setAttribute('src', src);
// Set iframe width for responsive behavior
//iframe.style.width = '100%';
//iframe.style.maxWidth = '100%';
}
}, 900);
});
});
});
I also encountered an issue with video thumbnails being cropped on smaller devices, which I resolved by adding aspect-ratio: 16/9;
and height: auto;
to the iframe in CSS.
Hope it helps someone.
-
This reply was modified 1 month, 1 week ago by gharchi.