Skip to content

Instantly share code, notes, and snippets.

@jdhobbsuk
Last active August 29, 2015 13:57
Show Gist options
  • Save jdhobbsuk/9834995 to your computer and use it in GitHub Desktop.
Save jdhobbsuk/9834995 to your computer and use it in GitHub Desktop.
YouTube / Vimeo shortcode for WordPress
// Video Shortcode
// -------------------------------------------------------------
function my_shortcode_video( $atts, $content = null ) {
// extract and set defaults
extract( shortcode_atts( array(
'url' => '',
'autoplay' => 'false',
'image' => 'false'
), $atts ) );
// Vimeo
if(preg_match('/vimeo.com/', $url)):
$type = 'vimeo';
$id = explode('vimeo.com/', $url);
$id = $id[1];
endif;
// YouTube
if( preg_match('/youtube.com/', $url) || preg_match('/youtu.be/', $url) ):
$type = 'youtube';
// Regular URL
if(preg_match('/youtube.com\/watch/', $url) ):
$id = explode('v=', $url);
$id = $id[1];
endif;
// Short URL (.be)
if(preg_match('/youtu.be/', $url)):
$id = explode('.be/', $url);
$id = $id[1];
endif;
// Embed URL
if(preg_match('/youtube.com\/embed/', $url) ):
$id = explode('/embed/', $url);
$id = $id[1];
endif;
endif;
if($type == 'youtube'):
switch($autoplay):
case 'true':
$play = '?autoplay=1';
break;
case 'check':
if($_GET['autoplay'] == 'true'):
$play = '?autoplay=1';
endif;
break;
endswitch;
if($image == 'true'):
$output_string = '<img src="http://img.youtube.com/vi/'.$id.'/0.jpg" alt="" />';
else:
$output_string = '<iframe src="//www.youtube.com/embed/'.$id.''.$play.'" frameborder="0" id="video" style="padding-top: 1em;"></iframe>';
endif;
elseif($type == 'vimeo'):
switch($autoplay):
case 'true':
$play = '&autoplay=1';
break;
case 'check':
if($_GET['autoplay'] == 'true'):
$play = '&autoplay=1';
endif;
break;
endswitch;
$output_string = '<iframe src="//player.vimeo.com/video/'.$id.'?title=0&amp;byline=0&amp;portrait=0'.$play.'" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen ></iframe>';
endif;
return force_balance_tags($output_string);
}
add_shortcode( 'video', 'my_shortcode_video' );
@jdhobbsuk
Copy link
Author

I've just added a new feature which allows you to set the video to autoplay.

It's a parameter of 'autoplay', which defaults to false and can be set to either true or check. 'Check' will look for the a URL parameter being set to true (?autoplay=true). This means you can add an autoplay to image thumbnails but not force external visitors to autoplay.

'True' will autoplay the video at all times.

[video url="http://youtu.be/AyAX9CU67I4" autoplay="check"]

@jdhobbsuk
Copy link
Author

I've just added a new feature which allows you to grab the thumbnail of the video (YouTube only, for now). This is useful for having YouTube handle the images instead of having to upload them manually into WordPress.

It's uses the parameter of 'image', which defaults to false. By setting the parameter to true, will grab the largest image possible and return the image ().

[video url="http://youtu.be/AyAX9CU67I4" image="true"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment