how to make video autoplay in widget

I'm using .mp4 video in widget as an alternative for GIF to speed up my load time. My question is, how can I make the video autoplay and looping without using a plugin?

Topic video-player widgets Wordpress

Category Web


You can do that by writing JavaScript. Like, in your theme footer (footer.php) if you place code like below (before the ending </body> tag):

<script>
    var vid = document.querySelector('video.wp-video-shortcode');
    vid.autoplay = true;
    vid.load();
</script>

It will autoplay all the videos of all the video widgets on the DOM (or in fact all the videos with the <video class="wp-video-shortcode"></video> tag). I just tested with the WordPress Video widget with a YouTube video.

If you want to autoplay only a certain video, then you have to use the id (#id) of the <video> element.

For the looping, you can consult this thread.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.