-
-
Save jdhobbsuk/9834995 to your computer and use it in GitHub Desktop.
| // 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&byline=0&portrait=0'.$play.'" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen ></iframe>'; | |
| endif; | |
| return force_balance_tags($output_string); | |
| } | |
| add_shortcode( 'video', 'my_shortcode_video' ); |
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"]
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"]
This shortcode will check whether the supplied URL is from YouTube or Vimeo, and produce the appropriate iframe. It can handle any URL from either service, so it's pretty idiot proof.
The shortcode looks as follows:
Note: This shortcode strips out the width and height attributes from the iframe, so whilst it's set for responsive, you might need something like FitVids to get it looking good.