Created
May 24, 2019 19:12
-
-
Save brycejacobson/8cb60a105c0a65dc890cb0d9b417a6a0 to your computer and use it in GitHub Desktop.
ACF Featured Videos Slider YouTube oEmbed
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 | |
/** | |
* Block Name: Featured Videos | |
* | |
* This is the template that displays Featured Videos block. | |
* | |
* @package Meta13_Functionality | |
* @since 1.4.0 | |
*/ | |
/** | |
* Enqueue the block's assets for the editor. | |
* | |
* @since 1.4.0 | |
*/ | |
$featured_videos_title = get_field( 'title' ); | |
$background_color = get_field( 'background_color' ); | |
?> | |
<section class="featured-videos has-<?php echo esc_attr( $background_color ); ?>-background-color alignfull"> | |
<div class="grid-container"> | |
<div class="featured-videos__title grid-x grid-margin-x"> | |
<div class="cell auto"> | |
<h2 class="line-after-title"><?php echo esc_attr( $featured_videos_title ); ?></h2> | |
</div> | |
</div><!-- .featured-videos__title --> | |
<div class="featured-videos__slider grid-x grid-padding-x"> | |
<?php if ( have_rows( 'videos' ) ) : ?> | |
<?php | |
while ( have_rows( 'videos' ) ) : | |
the_row(); | |
// vars. | |
$video = get_sub_field( 'video' ); | |
$video_link = get_sub_field( 'link' ); | |
// $vid = youtube_id_from_url( $video_url ); | |
$video_url = get_sub_field( 'video', false, false ); | |
// get wp_oEmed object, not a public method. new WP_oEmbed() would also be possible. | |
$oembed = _wp_oembed_get_object(); | |
// get provider. | |
$provider = $oembed->get_provider( $video_url ); | |
// fetch oembed data as an object. | |
$oembed_data = $oembed->fetch( $provider, $video_url ); | |
// $thumbnail = $oembed_data->thumbnail_url; | |
$video_title = $oembed_data->title; | |
// $iframe = $oembed_data->html; | |
// use preg_match to find iframe src. | |
preg_match( '/src="(.+?)"/', $video, $matches ); | |
$src = $matches[1]; | |
// add extra params to iframe src. | |
$params = array( | |
'modestbranding' => 1, | |
'rel' => 0, | |
'enablejsapi' => 1, // needed for pause to happen on change. | |
); | |
$new_src = add_query_arg( $params, $src ); | |
$video = str_replace( $src, $new_src, $video ); | |
// add extra attributes to iframe html. | |
$attributes = 'frameborder="0"'; | |
$video = str_replace( '></iframe>', ' ' . $attributes . '></iframe>', $video ); | |
?> | |
<div class="featured-video"> | |
<div class="responsive-embed widescreen"> | |
<?php echo $video; ?> | |
</div><!-- .responsive-embed --> | |
<div class="featured-video__info grid-x"> | |
<div class=" cell small-12 large-auto align-self-middle"> | |
<h3 class="featured-video__title"><?php echo esc_attr( $video_title ); ?></h3> | |
</div><!-- .featured-video__title --> | |
<div class="featured-video__link cell small-12 large-shrink align-self-middle"> | |
<?php if ( $video_link ) : ?> | |
<a href="<?php echo esc_url( $video_link['url'] ); ?>" class="button small" target="<?php echo esc_attr( $video_link['target'] ); ?>">Learn More</a> | |
<?php endif; ?> | |
</div><!-- .featured-video__link --> | |
</div><!-- .featured-video__info --> | |
</div><!-- .featured-video --> | |
<?php endwhile; ?> | |
<?php endif; ?> | |
</div><!-- .grid-x --> | |
</div><!-- .grid-container --> | |
</section><!-- .featured-videos --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment