Skip to content

Instantly share code, notes, and snippets.

@Soullighter
Last active October 7, 2019 13:08
Show Gist options
  • Save Soullighter/cff5b5cf557aa324f3dcd211cb22c9a2 to your computer and use it in GitHub Desktop.
Save Soullighter/cff5b5cf557aa324f3dcd211cb22c9a2 to your computer and use it in GitHub Desktop.
Parse Vimeo or YouTube url to get ID. ACF and Get thumbnail from video using vimeo API
// VIMEO
<?php
$url = get_field( 'full_video' );
preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $match);
$vimeo_id = $match[3];
$data = file_get_contents("http://vimeo.com/api/v2/video/$vimeo_id.json");
$data = json_decode($data);
?>
<img src="<?php echo $data[0]->thumbnail_large; ?>">
<iframe src="https://player.vimeo.com/video/<?php echo $vimeo_id; ?>?&loop=1&title=0&byline=0&portrait=0" style="position:absolute;top:0;left:0;width:100%;height:100%;" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
// YOUTUBE
<?php $url = get_field( 'yuotube_url' );
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$youtube_id = $match[1];
?>
<img src="http://i3.ytimg.com/vi/<?php echo $youtube_id ?>/maxresdefault.jpg">
<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php echo $youtube_id ?>?rel=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment