'Embed youtube "Shorts" videos
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&playsinline=1&controls=1&showinfo=0&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&playsinline=1&controls=1&showinfo=0&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&playsinline=1&controls=1&showinfo=0&modestbranding=0" frameborder="0" allowfullscreen></iframe>';
But no luck.
Solution 1:[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&playsinline=1&controls=1&showinfo=0&modestbranding=0" frameborder="0" allowfullscreen></iframe>';
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pupsik |
| Solution 2 | aurawindsurfing |
