germanny
11/14/2012 - 10:21 PM

Updated video function, now including YouTube (and Vimeo)

Updated video function, now including YouTube (and Vimeo)

<?php 
global $post;
$video_value = get_post_meta($post->ID, 'video_value', TRUE);
$video_string = $video_value; // url of the video; check if this is a vimeo or youtube video
	
if( strpos($video_string, 'vimeo') ) {

	$vimeo_video_id = substr($video_value, strrpos($video_value, '/')+1); // get just the id
	$vimeo_xml_file = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$vimeo_video_id.php")); // get the video data from the API
	$vimeo_video_width = $vimeo_xml_file[0]['width'];
	$vimeo_thumbnail_large = $vimeo_xml_file[0]['thumbnail_large']; // load up one of the image sizes
	
	if($vimeo_video_width < 1280) {
		$video_thumbnail = $vimeo_thumbnail_large;
	} else {
		$video_thumbnail = str_replace('_640.jpg', '_1280.jpg', $vimeo_thumbnail_large); // replace the size with the size we want
	}

} elseif( strpos($video_string, 'youtube') ) {

	$youtube_ID = '';
	if (preg_match('#^https?\://(?:(?:[a-z0-9-_\.]+\.|)youtube\.com/(?:watch\?v=|v/|embed/)|youtu\.be/)([a-z0-9-_]+)|^([a-z0-9-_]+)$#i', $video_value, $matches) > 0) {
		$youtube_ID = $matches[empty($matches[1]) ? 2 : 1];
	} else { }
	$video_thumbnail = 'http://img.youtube.com/vi/'.$youtube_ID.'/0.jpg';
	

} else {}


if ( have_posts() ) while ( have_posts() ) : the_post(); $currentPost = $post->ID; ?>

				<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
					<a href="<?php echo $video_value; ?>" class="video-in-link" style="background-image:url(<?php echo $video_thumbnail;?>);">

   <!--
			<?php
					display_video($video_value,1070,596);
					// echo '<p class="video-link">view on: <a href="'.$video_meta.'" title="Link to original video on Vimeo">'.$video_meta.'</a></p>';

			?>
			-->
				 		<b></b><?php // used as a coverup to hold fake video controls ?>
					</a>
		<?php endwhile; // end of the loop. ?>
<script type="text/javascript">
// This removes the comments from around the video playback function, allowing the video to load and play
// Also removes the empty <b>, used to contain fake video controls, so user can interact with the real video controls
//<![CDATA[
$(document).ready(function(){
	$("a.video-in-link").one('click',function(){
		var anchor = $(this);
		anchor.html(anchor.html().replace('<!--','').replace('-->',''));
		anchor.removeAttr('href');
		$(this).children('b').css('display','none');
		return false;
	})
})
//]]>
</script>
<?php

function display_video($video_meta,$video_width,$video_height) {
	
	$video_string = $video_meta; // url of the video; gonna use this to check if is a vimeo or youtube video
	
	if( strpos($video_string, 'vimeo') ) {

		/* =BEGIN: Insert Vimeo video from Custom Field ID
		Source: http://wordpress.org/extend/plugins/lux-vimeo-shortcode/
		Thanks Eric Rasch, @EricRasch
		---------------------------------------------------------------------------------------------------- */
		
			$strip_url = explode('/', $video_meta); // take the full string from the Custom Field
			$vimeo_ID = $strip_url[3]; // Strip out all the parts before the Vimeo ID.
			
			$clip_id = $vimeo_ID;
			$width = $video_width;
			$height = $video_height;
			$title = '1';
			$byline = '1';
			$portrait	= '1';
			$color = '';
			$html5 = '1';
			
			if (empty($clip_id) || !is_numeric($clip_id)) return '<!-- Vimeo: Invalid Vimeo ID -->';
			if ($height && !$width) $width = intval($height * 16 / 9);
			if (!$height && $width) $height = intval($width * 9 / 16);
			
			echo $html5 ?
			"<iframe src='http://player.vimeo.com/video/$clip_id?title=$title&amp;byline=$byline&amp;portrait=$portrait&amp;autoplay=1' width='$width' height='$height' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>" :
			"<object width='$width' height='$height'><param name='allowfullscreen' value='true' />".
			"<param name='allowscriptaccess' value='always' />".
			"<param name='movie' value='http://vimeo.com/moogaloop.swf?clip_id=$clip_id&amp;server=vimeo.com&amp;show_title=$title&amp;show_byline=$byline&amp;show_portrait=$portrait&amp;color=$color&amp;fullscreen=1' />".
			"<embed src='http://vimeo.com/moogaloop.swf?clip_id=$clip_id&amp;server=vimeo.com&amp;show_title=$title&amp;show_byline=$byline&amp;show_portrait=$portrait&amp;color=$color&amp;fullscreen=1' type='application/x-shockwave-flash' allowfullscreen='true' allowscriptaccess='always' width='$width' height='$height'></embed></object>".
			"<br /><a href='http://vimeo.com/$clip_id'>View on Vimeo</a>.";
	
	} elseif( strpos($video_string, 'youtube') ) {
	
		/* =BEGIN: Insert YouTube video from Custom Field ID
		With a little help from: http://wordpress.org/extend/plugins/youtube-shortcode/
		Still not sure if we need more from this, to be honest
		---------------------------------------------------------------------------------------------------- */
		
			 // Get the video's ID
			$youtube_ID = '';
			if (preg_match('#^https?\://(?:(?:[a-z0-9-_\.]+\.|)youtube\.com/(?:watch\?v=|v/|embed/)|youtu\.be/)([a-z0-9-_]+)|^([a-z0-9-_]+)$#i', $video_meta, $matches) > 0) {
				$youtube_ID = $matches[empty($matches[1]) ? 2 : 1];
			} else { }
			
			$clip_id = $youtube_ID;
			$width = $video_width;
			$height = $video_height;
			$html5 = '1';
		
			echo $html5 ?
			"<iframe width='$width' height='$height' src='http://www.youtube.com/embed/$clip_id?autoplay=1' frameborder='0' allowfullscreen></iframe>" :
			"<object width='$width' height='$height'>".
			"<param name='movie' value='http://www.youtube.com/v/$clip_id?version=3&amp;autoplay=1&amp;hl=en_US'></param>".
			"<param name='allowFullScreen' value='true'></param>
			<param name='allowscriptaccess' value='always'></param>".
			"<embed src='http://www.youtube.com/v/$clip_id?version=3&amp;hl=en_US' type='application/x-shockwave-flash' width='$width' height='$height' allowscriptaccess='always' allowfullscreen='true'></embed></object>".
			"<br><a href='http://www.youtube.com/watch?v=$clip_id'>View on YouTube</a>.";
			
	} else {}
}
?>