If you get a youtube URL by clicking "share" on a youtube video, you'll get a URL on the youtu.be domain, without the "v" URL parameter. The youtube shortcode provided by couchcms can't handle these links. Here's a replacement shortcode to add support for this type of youtube video link.
(Edit: I also added support for fluid video. I.e., if you set the "fluid" parameter to true (non-zero), the embedded video will adjust to the space available. maintaining a 16:9 aspect ratio. See https://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php.)
Plus, here's a shortcode to embed an html5 audio player, given a link to the audio file.
(Edit: I also added support for fluid video. I.e., if you set the "fluid" parameter to true (non-zero), the embedded video will adjust to the space available. maintaining a 16:9 aspect ratio. See https://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php.)
- Code: Select all
// YouTube Shortcode
// Usage: [youtube video="http://www.youtube.com/watch?v=5PsnxDQvQpw" fluid="1"]
// [youtube http://www.youtube.com/watch?v=1aBSPn2P9bg]
// [youtube 1aBSPn2P9bg]
$FUNCS->register_shortcode( 'youtube', 'youtube_handler' );
function youtube_handler( $params, $content=null ){
global $FUNCS;
extract( $FUNCS->get_named_vars(array(
'video' => 'http://',
'width' => '475',
'height' => '350',
'fluid' => false
), $params) );
// Video parameter is link or ID?
if ( (substr($video, 0, 7) == 'http://') || (substr($video, 0, 8) == 'https://') ){
/*
Example links that can be handled:
http://www.youtube.com/watch?v=5PsnxDQvQpw
http://youtube.com/watch?v=5PsnxDQvQpw
http://youtube.com/watch?gl=US&hl=en-US&v=5PsnxDQvQpw
http://youtube.com/v/5PsnxDQvQpw&rel=1
https://youtu.be/a1yt--_p5HM
*/
if( !preg_match('#https?://(?:www\.)?youtu.*(?:\?v=|&v=|/v/|/)([\w_-]+)#i', $video, $matches) ) return;
$video = $matches[1];
}
// Sanitize parameters
$video = htmlspecialchars( trim($video), ENT_QUOTES );
$width = (int)$width;
$height = (int)$height;
// Output HTML
$html = "";
$if_style = "";
if ($fluid) {
$html .= "<div style='position:relative; padding-bottom:56.25%; padding-top:25px; height:0;'>";
$if_style = "style='position: absolute; top: 0; left: 0; width: 100%; height: 100%;'";
}
$html .= "<iframe class='youtube-player' type='text/html' width='$width' height='$height' $if_style src='https://www.youtube.com/embed/${video}?rel=0' frameborder='0' allow='encrypted-media'></iframe>";
if ($fluid) $html .= "</div>";
return $html;
}
Plus, here's a shortcode to embed an html5 audio player, given a link to the audio file.
- Code: Select all
// Embedded html5 audio
// Usage: [audio5 audio="http://www.mysite.com/audio/audio file.mp3"]
// Usage: [audio5 audio="/audio/audio.mp3"]
$FUNCS->register_shortcode( 'audio5', 'audio5_handler' );
function audio5_handler( $params, $content=null ){
global $FUNCS;
extract( $FUNCS->get_named_vars(array(
'audio' => 'http://',
'type' => 'audio/mpeg'
), $params) );
$audio = htmlspecialchars( trim($audio), ENT_QUOTES );
$type = htmlspecialchars($type, ENT_QUOTES );
$html = "<audio controls> <source src='$audio' type='$type'> Your browser does not support the audio tag. </source></audio>";
return $html;
}