Skip to content

Instantly share code, notes, and snippets.

@mykt0ngc0
Forked from yratof/functions.php
Created June 17, 2019 19:44
Show Gist options
  • Save mykt0ngc0/44eeea1faaaf73fa7fcc0ea54c9bcf2c to your computer and use it in GitHub Desktop.
Save mykt0ngc0/44eeea1faaaf73fa7fcc0ea54c9bcf2c to your computer and use it in GitHub Desktop.

Revisions

  1. @yratof yratof created this gist Mar 10, 2015.
    132 changes: 132 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    <?php
    /* Pull apart OEmbed video link to get thumbnails out*/
    function get_video_thumbnail_uri( $video_uri ) {

    $thumbnail_uri = '';

    // determine the type of video and the video id
    $video = parse_video_uri( $video_uri );

    // get youtube thumbnail
    if ( $video['type'] == 'youtube' )
    $thumbnail_uri = 'http://img.youtube.com/vi/' . $video['id'] . '/hqdefault.jpg';

    // get vimeo thumbnail
    if( $video['type'] == 'vimeo' )
    $thumbnail_uri = get_vimeo_thumbnail_uri( $video['id'] );
    // get wistia thumbnail
    if( $video['type'] == 'wistia' )
    $thumbnail_uri = get_wistia_thumbnail_uri( $video_uri );
    // get default/placeholder thumbnail
    if( empty( $thumbnail_uri ) || is_wp_error( $thumbnail_uri ) )
    $thumbnail_uri = '';

    //return thumbnail uri
    return $thumbnail_uri;

    }


    /* Parse the video uri/url to determine the video type/source and the video id */
    function parse_video_uri( $url ) {

    // Parse the url
    $parse = parse_url( $url );

    // Set blank variables
    $video_type = '';
    $video_id = '';

    // Url is http://youtu.be/xxxx
    if ( $parse['host'] == 'youtu.be' ) {

    $video_type = 'youtube';

    $video_id = ltrim( $parse['path'],'/' );

    }

    // Url is http://www.youtube.com/watch?v=xxxx
    // or http://www.youtube.com/watch?feature=player_embedded&v=xxx
    // or http://www.youtube.com/embed/xxxx
    if ( ( $parse['host'] == 'youtube.com' ) || ( $parse['host'] == 'www.youtube.com' ) ) {

    $video_type = 'youtube';

    parse_str( $parse['query'] );

    $video_id = $v;

    if ( !empty( $feature ) )
    $video_id = end( explode( 'v=', $parse['query'] ) );

    if ( strpos( $parse['path'], 'embed' ) == 1 )
    $video_id = end( explode( '/', $parse['path'] ) );

    }

    // Url is http://www.vimeo.com
    if ( ( $parse['host'] == 'vimeo.com' ) || ( $parse['host'] == 'www.vimeo.com' ) ) {

    $video_type = 'vimeo';

    $video_id = ltrim( $parse['path'],'/' );

    }
    $host_names = explode(".", $parse['host'] );
    $rebuild = ( ! empty( $host_names[1] ) ? $host_names[1] : '') . '.' . ( ! empty($host_names[2] ) ? $host_names[2] : '');
    // Url is an oembed url wistia.com
    if ( ( $rebuild == 'wistia.com' ) || ( $rebuild == 'wi.st.com' ) ) {

    $video_type = 'wistia';

    if ( strpos( $parse['path'], 'medias' ) == 1 )
    $video_id = end( explode( '/', $parse['path'] ) );

    }

    // If recognised type return video array
    if ( !empty( $video_type ) ) {

    $video_array = array(
    'type' => $video_type,
    'id' => $video_id
    );

    return $video_array;

    } else {

    return false;

    }

    }


    /* Takes a Vimeo video/clip ID and calls the Vimeo API v2 to get the large thumbnail URL.*/
    function get_vimeo_thumbnail_uri( $clip_id ) {
    $vimeo_api_uri = 'http://vimeo.com/api/v2/video/' . $clip_id . '.php';
    $vimeo_response = wp_remote_get( $vimeo_api_uri );
    if( is_wp_error( $vimeo_response ) ) {
    return $vimeo_response;
    } else {
    $vimeo_response = unserialize( $vimeo_response['body'] );
    return $vimeo_response[0]['thumbnail_large'];
    }

    }

    /* Takes a wistia oembed url and gets the video thumbnail url. */
    function get_wistia_thumbnail_uri( $video_uri ) {
    if ( empty($video_uri) )
    return false;
    $wistia_api_uri = 'http://fast.wistia.com/oembed?url=' . $video_uri;
    $wistia_response = wp_remote_get( $wistia_api_uri );
    if( is_wp_error( $wistia_response ) ) {
    return $wistia_response;
    } else {
    $wistia_response = json_decode( $wistia_response['body'], true );
    return $wistia_response['thumbnail_url'];
    }
    }
    25 changes: 25 additions & 0 deletions page.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    <?php
    //For Use with Repeater Field
    $videos = get_field('videos');
    $videos_raw = get_field('videos', FALSE, FALSE);


    //Add the Thubmnail to the $videos object
    foreach($videos_raw as $key => $video_raw) :
    $videos[$key]['video_thumb'] = get_video_thumbnail_uri($video_raw['field_5449746362c3d']);
    // Replace 'field_5449746362c3d' with your field's Field key
    // (obtainable by going to screen options in the fields admin,
    // and setting 'Show Field Key' to 'Yes')
    endforeach;

    //Loop through the $videos object
    foreach($videos as $video): //Lightbox Link via Thumbnail ?>

    <a href="#lightbox"><img src="<?php echo $video['video_thumb']; ?>"/></a>

    <?php //Lightbox Element with Video Embed Code ?>
    <div id="lightbox" class="embed-container">
    <?php echo $video['video'] ?>
    </div>

    <?php endforeach; ?>
    14 changes: 14 additions & 0 deletions single.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?php
    //For use with a single field.
    $video = get_field('video'); //Embed Code
    $video_url = get_field('video', FALSE, FALSE); //URL

    $video_thumb_url = get_video_thumbnail_uri($video_url); //get THumbnail via our functions in functions.php ?>

    <?php //Lightbox Link via Thumbnail ?>
    <a href="#lightbox"><img src="<?php echo $video_thumb_url; ?>"/></a>

    <?php //Lightbox Element with Video Embed Code ?>
    <div id="lightbox" class="embed-container">
    <?php echo $video; ?>
    </div>