Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save artlung/407022 to your computer and use it in GitHub Desktop.
Save artlung/407022 to your computer and use it in GitHub Desktop.
<?php
// Add this to functions.php in your theme to allow youtube
// video shortcodes like wordpress.com does:
// http://en.support.wordpress.com/videos/youtube/
function youtube_shortcode_func($atts) {
extract(shortcode_atts(array(
'youtube' => '',
), $atts));
$url = $atts[0];
$url_p = parse_url($url);
$url_q = $url_p['query'];
$q_parts = explode('&amp;', $url_q);
$youtube = array();
$legal_parts = array('v', 'w', 'h', 'hd', 'showsearch', 'rel', 'fs');
foreach($q_parts as $str) {
list($key, $value) = explode('=', $str);
if (in_array($key, $legal_parts)) {
$youtube[$key] = $value;
}
}
(!isset($youtube['rel'])) {
$youtube['rel'] = 0;
}
(!isset($youtube['fs'])) {
$youtube['fs'] = 1;
}
return "<object width=\"{$youtube['w']}\" height=\"{$youtube['h']}\"><param name=\"movie\" value=\"http://www.youtube.com/v/{$youtube['v']}&fs={$youtube['fs']}&rel={$youtube['rel']}\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/{$youtube['v']}&fs={$youtube['fs']}&rel={$youtube['rel']}\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"{$youtube['w']}\" height=\"{$youtube['h']}\"></embed></object>";
}
add_shortcode('youtube', 'youtube_shortcode_func');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment