Skip to content

Instantly share code, notes, and snippets.

@collegeman
Forked from coreyweb/youtube-shortcode.php
Created March 6, 2012 17:03
Show Gist options
  • Select an option

  • Save collegeman/1987525 to your computer and use it in GitHub Desktop.

Select an option

Save collegeman/1987525 to your computer and use it in GitHub Desktop.

Revisions

  1. collegeman renamed this gist Mar 6, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. collegeman revised this gist Mar 6, 2012. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions youtube wordpress shortcode
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    /**
    * Youtube Shortcode Solution
    *
    @@ -14,14 +15,18 @@
    function youtube($atts) {
    extract(shortcode_atts(array(
    "value" => 'http://',
    "width" => '590',
    // "height" => '350'
    "width" => '590'
    ), $atts));

    if ($oembed = coreylib('http://www.youtube.com/oembed?url='.urlencode($value).'&format=xml','1 month')) {

    preg_match('#vi/(.*)/#i', $oembed->get('thumbnail_url'), $matches);
    $id = $matches[1];
    if (preg_match('#vi/(.*)/#i', $oembed->get('thumbnail_url'), $matches)) {
    $id = $matches[1];
    } else if (preg_match('#v=(.*)&?#i', $oembed->get('thumbnail_url'), $matches)) {
    $id = $matches[1];
    } else {
    return '<a href="'.$value.'">'.$value.'</a>';
    }

    $newHeight = round($width * (string) $oembed->get('height') / (string) $oembed->get('width'));

  3. @coreyweb coreyweb renamed this gist Mar 6, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @coreyweb coreyweb created this gist Mar 6, 2012.
    33 changes: 33 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    /**
    * Youtube Shortcode Solution
    *
    * Set your desired width for all the videos on your site (below)
    * Paste any Youtube video link in the shortcode, using this format, i.e.:
    * [youtube value="http://www.youtube.com/watch?feature=player_embedded&v=GGT8ZCTBoBA"]
    * This code will automatically resize your video to the appropriate size and aspect ratio.
    * Modify the width value (set to 590 here), and the iframe code below to your desired settings.
    * This requires coreylib (coreylib.com)
    * Shortcode by coreyweb: https://github.com/coreyweb
    * and collegeman: https://github.com/collegeman
    */

    function youtube($atts) {
    extract(shortcode_atts(array(
    "value" => 'http://',
    "width" => '590',
    // "height" => '350'
    ), $atts));

    if ($oembed = coreylib('http://www.youtube.com/oembed?url='.urlencode($value).'&format=xml','1 month')) {

    preg_match('#vi/(.*)/#i', $oembed->get('thumbnail_url'), $matches);
    $id = $matches[1];

    $newHeight = round($width * (string) $oembed->get('height') / (string) $oembed->get('width'));

    return '<iframe width="'.$width.'" height="'.$newHeight.'" src="http://www.youtube.com/embed/'.$id.'?rel=0&amp;wmode=transparent" frameborder="0" allowfullscreen></iframe>';

    }
    }

    add_shortcode("youtube", "youtube");