Skip to content

Instantly share code, notes, and snippets.

@dobsondev
Created March 25, 2014 18:01
Show Gist options
  • Save dobsondev/9767514 to your computer and use it in GitHub Desktop.
Save dobsondev/9767514 to your computer and use it in GitHub Desktop.
Shortcode for Embedding a YouTube Video - Used in DobsonDev Shortcodes Plugin for WordPress
<?php
/* Adds a shortcode to embed a YouTube video */
function dobson_embed_youtube($atts) {
extract(shortcode_atts(array(
'video' => "Invalid Video ID",
'width' => "560",
'height' => "315",
), $atts));
$source_headers = @get_headers("http://youtube.com/watch?v=" . $video);
if (strpos($source_headers[0], '404 Not Found')) {
return '<p> Invalid YouTube video ID. Please check your YouTube video ID. </p>';
} else {
return '<iframe width="' . $width . '" height="' . $height . '" src="//www.youtube.com/embed/' . $video
. '" frameborder="0" allowfullscreen></iframe>';
}
}
add_shortcode('embedYouTube', 'dobson_embed_youtube');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment