SHARE
TWEET

Youtube shortcode for WordPress

aldolat Jan 15th, 2013 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Embed a Youtube video.
  3.  *
  4.  * You can use either the complete form of the URL provided by Youtube
  5.  * or only the ID of the video.
  6.  *
  7.  * In the feed, the shortcode displays a poster frame of the video linked to the original post.
  8.  *
  9.  * The only necessary parameter is $id.
  10.  *
  11.  * @param string $id The ID of the video, that could be either the complete URL or only the ID.
  12.  * @param string $width The width of the player.
  13.  * @param string $height The height of the player.
  14.  * @param string $hour The starting hour of the video
  15.  * @param string $min The starting minute of the video
  16.  * @param string $sec The starting second of the video
  17.  * @param string $autoplay Specify if the player will autoplay the video.
  18.  * @param string $class Specify the class for the container P HTML element
  19.  *
  20.  * @example [youtubesc id=Gf69cRh01Q0]
  21.  * @example [youtubesc id=http://www.youtube.com/watch?v=Gf69cRh01Q0]
  22.  * @example [youtubesc id=http://www.youtube.com/watch?v=Gf69cRh01Q0 sec=55]
  23.  * @example [youtubesc id=http://www.youtube.com/watch?v=Gf69cRh01Q0 width=460 min=12]
  24.  * @example [youtubesc id=http://www.youtube.com/watch?v=Gf69cRh01Q0 width=460 height=372 hour=1 min=5 sec=9 autoplay=1]
  25.  *
  26.  * @since 1.0
  27.  */
  28.  
  29. add_shortcode('youtubesc', 'ubnsc_youtube');
  30.  
  31. function ubnsc_youtube( $atts ) {
  32.         extract( shortcode_atts( array(
  33.                 'id'       => '',
  34.                 'width'    => '480',
  35.                 'height'   => '360',
  36.                 'hour'     => 0,
  37.                 'min'      => 0,
  38.                 'sec'      => 0,
  39.                 'autoplay' => 0,
  40.                 'class'    => '',
  41.         ), $atts ) );
  42.  
  43.         // Sanitize some options
  44.         $width    = absint( intval( $width ) );
  45.         $height   = absint( intval( $height ) );
  46.         $hour     = absint( intval( $hour ) );
  47.         $min      = absint( intval( $min ) );
  48.         $sec      = absint( intval( $sec ) );
  49.         $autoplay = absint( intval( $autoplay ) );
  50.         if ( $autoplay < 0 || $autoplay > 1 ) $autoplay = 0;
  51.  
  52.         // Figure out if the user is using the complete form of the URL or only the ID of the video
  53.         $pos = strpos( $id, 'http://' );
  54.         if ( $pos === false ) {
  55.                 $videoid = $id;
  56.         } else {
  57.                 $videoid = substr( esc_url( $id ), 31, 11 );
  58.         }
  59.  
  60.         if ( $class ) {
  61.                 $element = '<p class="' . $class . '">';
  62.         } else {
  63.                 $element = '<p>';
  64.         }
  65.  
  66.         if ( ! is_feed() ) {
  67.                 // Convert hours and minutes in seconds
  68.                 $start = ( ( $hour * 60 * 60 ) + ( $min * 60 ) + $sec );
  69.                 $output = $element . '<object style="display:block; margin:0 auto;" type="application/x-shockwave-flash" data="http://www.youtube.com/v/' . $videoid . '?fs=1&amp;hl=en_US&amp;start=' . $start . '&amp;autoplay=' . $autoplay. '" width="' . $width . '" height="' . $height . '">
  70.                                         <param name="movie" value="http://www.youtube.com/v/' . $videoid . '?fs=1&amp;hl=en_US&amp;start=' . $start . '" />
  71.                                         <param name="FlashVars" value="playerMode=embedded" />
  72.                                         <param name="allowFullScreen" value="true" />
  73.                                         <param name="allowscriptaccess" value="always" />
  74.                                         <param name="wmode" value="transparent" />
  75.                                         </object></p>';
  76.         } else {
  77.                 $output = $element . '<a href="' . get_permalink() . '" title="' . __( 'Click here to view the embedded video', 'domain' ) . '">
  78.                                        <img src="http://i3.ytimg.com/vi/' . $videoid . '/0.jpg" alt="' . __( 'Video preview', 'domain' ) . '" />
  79.                                    </a></p>';
  80.         }
  81.         return $output;
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top