1

I've come across a bug on a clients site. All the youtube embeds in their new posts are now set at width="200" and height="113", however some of the older posts have videos embedded at width="612" by height="344".

Has something changed in the Ghost codebase that now sets the embeds at this size? I have tried to copy the old posts videos into the new posts, just incase there is an issue with the youtube video. However it still sets it at the smaller size.

Graeme Paul
  • 1,143
  • 1
  • 16
  • 30

2 Answers2

1

Here is a way where you can embed any youtube video in Ghost without specifying the height and width. Your embedded video will be responsive on any device and the loading time will be very less.

Add this script to your page by going in the code injection

<script>
  /*
   * Light YouTube Embeds by @labnol
   * Credit: https://www.labnol.org/
   */

  function labnolIframe(div) {
    var iframe = document.createElement('iframe');
    iframe.setAttribute(
      'src',
      'https://www.youtube.com/embed/' + div.dataset.id + '?autoplay=1&rel=0'
    );
    iframe.setAttribute('frameborder', '0');
    iframe.setAttribute('allowfullscreen', '1');
    iframe.setAttribute(
      'allow',
      'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture'
    );
    div.parentNode.replaceChild(iframe, div);
  }

  function initYouTubeVideos() {
    var playerElements = document.getElementsByClassName('youtube-player');
    for (var n = 0; n < playerElements.length; n++) {
      var videoId = playerElements[n].dataset.id;
      var div = document.createElement('div');
      div.setAttribute('data-id', videoId);
      var thumbNode = document.createElement('img');
      thumbNode.src = '//i.ytimg.com/vi/ID/hqdefault.jpg'.replace(
        'ID',
        videoId
      );
      div.appendChild(thumbNode);
      var playButton = document.createElement('div');
      playButton.setAttribute('class', 'play');
      div.appendChild(playButton);
      div.onclick = function () {
        labnolIframe(this);
      };
      playerElements[n].appendChild(div);
    }
  }

  document.addEventListener('DOMContentLoaded', initYouTubeVideos);
</script>

and add little bit CSS for styling

.youtube-player {
    position: relative;
    padding-bottom: 56.25%;
    height: 0;
    overflow: hidden;
    max-width: 100%;
    background: #000;
    margin: 5px;
  }

  .youtube-player iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 100;
    background: transparent;
  }

  .youtube-player img {
    object-fit: cover;
    display: block;
    left: 0;
    bottom: 0;
    margin: auto;
    max-width: 100%;
    width: 100%;
    position: absolute;
    right: 0;
    top: 0;
    border: none;
    height: auto;
    cursor: pointer;
    -webkit-transition: 0.4s all;
    -moz-transition: 0.4s all;
    transition: 0.4s all;
  }

  .youtube-player img:hover {
    -webkit-filter: brightness(75%);
  }

  .youtube-player .play {
    height: 72px;
    width: 72px;
    left: 50%;
    top: 50%;
    margin-left: -36px;
    margin-top: -36px;
    position: absolute;
    background: url('//i.imgur.com/TxzC70f.png') no-repeat;
    cursor: pointer;
  }

Now you just have to put one line to embedded youtube video in ghost

<div class="youtube-player" data-id="VIDEO_ID"></div>

Do not forget to add your video id at the place of VIDEO_ID

thelovekesh
  • 1,364
  • 1
  • 8
  • 22
  • Thank you, I used aspect-ratio and changed `.youtube-player { position: relative; aspect-ratio: 16 / 9; width: 100%; overflow: hidden; background: #000; margin: 5px; }` – zx81roadkill Jan 07 '23 at 13:20
  • And, for privacy the embed URL can be www.youtube-nocookie.com in `'https://www.youtube-nocookie.com/embed/' + div.dataset.id + '?autoplay=1&rel=0' ` – zx81roadkill Jan 07 '23 at 15:20
0

I use this simple js code:

window.on('load', function() {
    $('.kg-card.kg-embed-card > iframe').css({height: "344px"})
}

Card classes belong to 2.16.4
Find the generated classes in a sample post if you use another Ghost version.

Evhz
  • 8,852
  • 9
  • 51
  • 69