Skip to content

Instantly share code, notes, and snippets.

@kjantzer
Created December 13, 2014 18:36
Show Gist options
  • Save kjantzer/f568f1fc2b77d71c5294 to your computer and use it in GitHub Desktop.
Save kjantzer/f568f1fc2b77d71c5294 to your computer and use it in GitHub Desktop.
WordPress: YouTube shortcode
// http://embedresponsively.com/
.embed-container {
position: relative;
padding-bottom: 56.25%;
height: 0;
overflow: hidden;
max-width: 100%;
height: auto;
margin-bottom: 1.5em;
}
.embed-container iframe,
.embed-container object,
.embed-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
function kj_youtube($a) {
extract(shortcode_atts(array(
'url' => 'http://www.youtube.com/watch?v=A1P_nRxZXl4'
), $a));
// globals for Open Graph use
global $youtube_id;
global $youtube_thumbnail;
global $youtube_url;
global $youtube_url_secure;
// http://stackoverflow.com/a/9102270
preg_match("/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/", $url, $matches);
if( isset($matches[2]) ){
$youtube_id = $matches[2];
$youtube_thumbnail = "http://img.youtube.com/vi/$youtube_id/hqdefault.jpg";
$youtube_url = "http://www.youtube.com/v/$youtube_id";
$youtube_url_secure = "https://www.youtube.com/v/$youtube_id";
$url = "http://www.youtube.com/embed/$youtube_id";
}
return "<div class='embed-container'><iframe src='$url' frameborder='0' allowfullscreen></iframe></div>";
}
add_shortcode('youtube', 'kj_youtube');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment