Embed youtube "Shorts" videos
Asked Answered
B

3

5

I have this code and it works just fine.

if( strcasecmp( 'www.youtube.com/watch', $link[2] ) == 0 && $this->params( $params, $link[3], 'v' ) )
      return '<iframe width="'.$width.'" height="'.$height.'"  src="'.$link[1].'www.youtube.com/embed/'.$params['v'].'?rel=0&amp;playsinline=1&amp;controls=1&amp;showinfo=0&amp;modestbranding=0" frameborder="0" allowfullscreen></iframe>';
    else if( preg_match( '/^(?:www\.)?youtu\.be\/([^\/]+)/i', $link[2], $matches ))
      return '<iframe width="'.$width.'" height="'.$height.'"  src="'.$link[1].'www.youtube.com/embed/'.$matches[1].'?rel=0&amp;playsinline=1&amp;controls=1&amp;showinfo=0&amp;modestbranding=0" frameborder="0" allowfullscreen></iframe>'; 

And it works just fine, with auto replace links with video. But now, youtube have this thing where short videos have a link with "Shorts"

Example: https://youtube.com/shorts/d1wXX9xO_2o?feature=share

That will not embed automatically. Any idea how to fix this?

I have tried:

else if( preg_match( '/^(?:www\.)?youtube\.com/shorts\/([^\/]+)/i', $link[2], $matches ))
          return '<iframe width="'.$width.'" height="'.$height.'"  src="'.$link[1].'www.youtube.com/embed/'.$matches[1].'?rel=0&amp;playsinline=1&amp;controls=1&amp;showinfo=0&amp;modestbranding=0" frameborder="0" allowfullscreen></iframe>'; 

But no luck.

Burwell answered 7/4, 2022 at 21:50 Comment(2)
You have to change/update your regex for extract its video_id.Vacla
But it does grab the video ID, which is d1wXX9xO_2o. The upper code does work with extracting a video from youtube just fine. I just cant seem to add "shorts" in there.Burwell
B
1

This works.

else if( preg_match( '/youtube\.com\/shorts\/(\w+\s*\/?)*([0-9]+)*$/i', $link[2], $matches ) )
        return '<iframe width="'.$width.'" height="'.$height.'"  src="'.$link[1].'www.youtube.com/embed/'.$matches[1].'?rel=0&amp;playsinline=1&amp;controls=1&amp;showinfo=0&amp;modestbranding=0" frameborder="0" allowfullscreen></iframe>';
Burwell answered 9/4, 2022 at 12:59 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Lonlona
Thanks. Just for others, I had to change preg match "#^http(s?)\://www\.youtube\.com/shorts/(.*)#i".Arvy
R
4

You do not need to use different code for shorts just use the identifier from the end of their url and this iframe:

<iframe
 width="720"
 height="576"
 src="{{'https://www.youtube.com/embed/' . $video}}"
 title="YouTube video player"
 frameborder="0"
 allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
 allowfullscreen>
</iframe>

tested and it work perfectly for me.

Renita answered 2/6, 2022 at 12:2 Comment(0)
N
3

I tried this solution by Document360, and it worked.

Link to solution:

How to embed YouTube Shorts?

Copying the video link from the address bar, click on Share, and replace "/shorts/" with "/embed/" in the URL. This will allow you to embed any YouTube Shorts in your page or article.

Neophyte answered 15/6, 2023 at 2:52 Comment(0)
B
1

This works.

else if( preg_match( '/youtube\.com\/shorts\/(\w+\s*\/?)*([0-9]+)*$/i', $link[2], $matches ) )
        return '<iframe width="'.$width.'" height="'.$height.'"  src="'.$link[1].'www.youtube.com/embed/'.$matches[1].'?rel=0&amp;playsinline=1&amp;controls=1&amp;showinfo=0&amp;modestbranding=0" frameborder="0" allowfullscreen></iframe>';
Burwell answered 9/4, 2022 at 12:59 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Lonlona
Thanks. Just for others, I had to change preg match "#^http(s?)\://www\.youtube\.com/shorts/(.*)#i".Arvy

© 2022 - 2024 — McMap. All rights reserved.