-
-
Save em-piguet/661c1ea52f934a32cc22f9bd9298b03d to your computer and use it in GitHub Desktop.
Youtube Video gallery acf oEmbed field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_youtube_video_ID($youtube_video_url) { | |
/** | |
* Pattern matches | |
* http://youtu.be/ID | |
* http://www.youtube.com/embed/ID | |
* http://www.youtube.com/watch?v=ID | |
* http://www.youtube.com/?v=ID | |
* http://www.youtube.com/v/ID | |
* http://www.youtube.com/e/ID | |
* http://www.youtube.com/user/username#p/u/11/ID | |
* http://www.youtube.com/leogopal#p/c/playlistID/0/ID | |
* http://www.youtube.com/watch?feature=player_embedded&v=ID | |
* http://www.youtube.com/?feature=player_embedded&v=ID | |
*/ | |
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $youtube_video_url, $match)){ | |
return $match[1]; | |
} | |
// if no match return false. | |
return false; | |
} | |
if ( have_rows( 'videos' ) ):?> | |
<div id="videos" class="empVideos"> | |
<h3 class="sec-title"> | |
Galeria de vídeos | |
</h3> | |
<div class="videos"> | |
<?php $count = 1; | |
while ( have_rows( 'videos' ) ) : the_row(); | |
$iframe = get_sub_field( 'video' ); | |
// use preg_match to find iframe src | |
preg_match( '/src="(.+?)"/', $iframe, $matches ); | |
$src = $matches[1]; | |
$yt = get_youtube_video_ID( $src ); | |
if ( $yt ): | |
if ( $count % 3 == 1 ) { | |
echo "<div class=\"col-md-12 mx-auto \"><div class=\"d-flex flex-row justify-content-center align-items-center\">"; | |
} | |
?> | |
<a class="fancybox mx-auto p-2" href="<?= $src; ?>"> | |
<img src="http://img.youtube.com/vi/<?= $yt; ?>/hqdefault.jpg" width="250" img-fluid"> | |
<span class="play"> | |
<i class="fa fa-play-circle"></i> | |
</span> | |
</a> | |
<?php | |
if ( $count % 3 == 0 ) { | |
echo "</div>"; | |
echo "</div>"; | |
} | |
$count ++; | |
endif; | |
endwhile; | |
if ( $count % 3 != 1 ) { | |
echo "</div>"; | |
echo "</div>"; | |
} ?> | |
</div> | |
</div> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment