How To Automatically Embed The Latest Video From A YouTube Channel?

Embed the latest or second latest video from any youtube channel using javascript method or iframe method.
automatically embed latest video from youtube channel

If you happen to own both a blog and a youtube channel, then you might have a “My Youtube Channel” widget on your site to display your latest videos. But updating this widget every time you upload a video can be a hefty and monotonous work. Not anymore! Now you can automatically embed the latest video from a youtube channel with a single line of code.

Auto Embed Latest Video from a Youtube Channel

<iframe width="600" height="340" src="" frameborder="0" allowfullscreen></iframe>

In the above code snippet, replace the channel name placeholder with your channel name. The snippet won’t work with channel IDs so make sure to add the correct channel name itself.

Copy-paste the code into an online HTML code editor to see its working.

Auto Embed Latest/Second Latest Video from Youtube Channel

With this method, you can embed the latest, second latest or third latest video from any youtube channel by knowing it’s channel ID. The channel ID should be replaced in the cid parameter of the iframe. To load the latest video use the parameter vnum='0' and for second latest use vnum='1' and so on.

const requestOptions = {
    method: 'GET',
    redirect: 'follow'

const loadVideo = (iframe) => {
    const cid = iframe.getAttribute('cid');
    const channelURL = encodeURIComponent(`${cid}`)
    const reqURL = `${channelURL}`;
    fetch(reqURL, requestOptions)
        .then(response => response.json())
        .then(result => {
            const videoNumber = (iframe.getAttribute('vnum') ? Number(iframe.getAttribute('vnum')) : 0);
            const link = result.items[videoNumber].link;
            const id = link.substr(link.indexOf("=") + 1);
            iframe.setAttribute("src", `${id}?controls=0&autoplay=1`);
        .catch(error => console.log('error', error));
var iframes = document.getElementsByClassName('latestVideoEmbed');
for (var i = 0, len = iframes.length; i < len; i++) {

See the code in action in the here.

If this worked for you, please leave an upvote to my answer at StackOverflow.

Related Posts