I’m trying to bulk upload a video on multiple WordPress websites at once by just uploading the video on a server.
Everything is working fine except the video doesn’t load on single post’s sidebar.
The idea is that the video loads on many websites at once only when we have a video available on the server and when there’s no video available the video player hides, that’s why the video is hidden at first and then get’s visible via the script only when there’s no error on the video player.
The time() function is used to tackle the cache issue.
The script is unable to execute in single post’s sidebar only (where I actually want the video to show), as the video is getting loaded on Homepage’s sidebar and even after the content on single posts perfectly.
Code is placed using the plugin Advanced Ads and placed via it’s widget on both sidebars (Homepage sidebar and Sidebar for Posts).
Here’s the code –
<video id="video" width="100%" height="auto" poster="https://webtik.in/ads/cover.jpeg?nocache=<?php echo time(); ?>" controls style="display: none;">
<source id="videoSource" src="https://webtik.in/ads/video.mp4?nocache=<?php echo time(); ?>" type="video/mp4">
</video>
<script>
document.addEventListener('DOMContentLoaded', function () {
const checkAndInitializeVideo = () => {
const video = document.getElementById('video');
const videoSource = document.querySelector('#video source');
if (video && videoSource) {
video.addEventListener('loadedmetadata', () => {
video.style.display = 'block';
});
video.addEventListener('error', () => {
video.style.display = 'none';
});
videoSource.addEventListener('error', () => {
video.style.display = 'none';
});
}
};
checkAndInitializeVideo();
const sidebar = document.querySelector('#secondary');
if (sidebar) {
const observer = new MutationObserver(() => {
checkAndInitializeVideo();
});
observer.observe(sidebar, { childList: true, subtree: true });
}
});
</script>
After Googling a bit I came to know that the sidebar is loading dynamically on single pages, hence the script is not getting executed. Any fix for this?

