youtube_id

Converts a YouTube URL into an embedded YouTube video.

Description

youtube_id( $url ); 

Parameters (1)

0. $url
The url.

Usage

  1. if ( !function_exists( 'youtube_id' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/shortcodes/youtube.php'; 
  3.  
  4. // The url. 
  5. $url = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = youtube_id($url); 
  9.  

Defined (1)

The function is defined in the following location(s).

/modules/shortcodes/youtube.php  
  1. function youtube_id( $url ) { 
  2. if ( ! $id = jetpack_get_youtube_id( $url ) ) 
  3. return '<!--YouTube Error: bad URL entered-->'; 
  4.  
  5. $url = youtube_sanitize_url( $url ); 
  6. $url = parse_url( $url ); 
  7.  
  8. if ( ! isset( $url['query'] ) ) 
  9. return false; 
  10.  
  11. if ( isset( $url['fragment'] ) ) { 
  12. wp_parse_str( $url['fragment'], $fargs ); 
  13. } else { 
  14. $fargs = array(); 
  15. wp_parse_str( $url['query'], $qargs ); 
  16.  
  17. $qargs = array_merge( $fargs, $qargs ); 
  18.  
  19. // calculate the width and height, taking content_width into consideration 
  20. global $content_width; 
  21.  
  22. $input_w = ( isset( $qargs['w'] ) && intval( $qargs['w'] ) ) ? intval( $qargs['w'] ) : 0; 
  23. $input_h = ( isset( $qargs['h'] ) && intval( $qargs['h'] ) ) ? intval( $qargs['h'] ) : 0; 
  24.  
  25. $default_width = get_option('embed_size_w'); 
  26.  
  27. if ( empty( $default_width ) ) { 
  28. if ( ! empty( $content_width ) ) { 
  29. $default_width = $content_width; 
  30. } else { 
  31. $default_width = 640; 
  32.  
  33. if ( $input_w > 0 && $input_h > 0 ) { 
  34. $w = $input_w; 
  35. $h = $input_h; 
  36. } elseif ( 0 == $input_w && 0 == $input_h ) { 
  37. if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) { 
  38. $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 ); 
  39. } else { 
  40. $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width ); 
  41. $h = ceil( ( $w / 16 ) * 9 ) + 30; 
  42. } elseif ( $input_w > 0 ) { 
  43. $w = $input_w; 
  44. $h = ceil( ( $w / 16 ) * 9 ) + 30; 
  45. } else { 
  46. if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) { 
  47. $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 ); 
  48. } else { 
  49. $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width ); 
  50. $h = $input_h; 
  51.  
  52. /** 
  53. * Filter the YouTube player width. 
  54. * @module shortcodes 
  55. * @since 1.1.0 
  56. * @param int $w Width of the YouTube player in pixels. 
  57. */ 
  58. $w = (int) apply_filters( 'youtube_width', $w ); 
  59.  
  60. /** 
  61. * Filter the YouTube player height. 
  62. * @module shortcodes 
  63. * @since 1.1.0 
  64. * @param int $h Height of the YouTube player in pixels. 
  65. */ 
  66. $h = (int) apply_filters( 'youtube_height', $h ); 
  67.  
  68. $rel = ( isset( $qargs['rel'] ) && 0 == $qargs['rel'] ) ? 0 : 1; 
  69. $search = ( isset( $qargs['showsearch'] ) && 1 == $qargs['showsearch'] ) ? 1 : 0; 
  70. $info = ( isset( $qargs['showinfo'] ) && 0 == $qargs['showinfo'] ) ? 0 : 1; 
  71. $iv = ( isset( $qargs['iv_load_policy'] ) && 3 == $qargs['iv_load_policy'] ) ? 3 : 1; 
  72.  
  73. $fmt = ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) ? '&fmt=' . (int) $qargs['fmt'] : ''; 
  74.  
  75. if ( ! isset( $qargs['autohide'] ) || ( $qargs['autohide'] < 0 || 2 < $qargs['autohide'] ) ) { 
  76. $autohide = '&autohide=2'; 
  77. } else { 
  78. $autohide = '&autohide=' . absint( $qargs['autohide'] ); 
  79.  
  80. $start = 0; 
  81. if ( isset( $qargs['start'] ) ) { 
  82. $start = intval( $qargs['start'] ); 
  83. } else if ( isset( $qargs['t'] ) ) { 
  84. $time_pieces = preg_split( '/(?<=\D)(?=\d+)/', $qargs['t'] ); 
  85.  
  86. foreach ( $time_pieces as $time_piece ) { 
  87. $int = (int) $time_piece; 
  88. switch ( substr( $time_piece, -1 ) ) { 
  89. case 'h' : 
  90. $start += $int * 3600; 
  91. break; 
  92. case 'm' : 
  93. $start += $int * 60; 
  94. break; 
  95. case 's' : 
  96. $start += $int; 
  97. break; 
  98.  
  99. $start = $start ? '&start=' . $start : ''; 
  100. $end = ( isset( $qargs['end'] ) && intval( $qargs['end'] ) ) ? '&end=' . (int) $qargs['end'] : ''; 
  101. $hd = ( isset( $qargs['hd'] ) && intval( $qargs['hd'] ) ) ? '&hd=' . (int) $qargs['hd'] : ''; 
  102.  
  103. $vq = ( isset( $qargs['vq'] ) && in_array( $qargs['vq'], array('hd720', 'hd1080') ) ) ? '&vq=' . $qargs['vq'] : ''; 
  104.  
  105. $cc = ( isset( $qargs['cc_load_policy'] ) ) ? '&cc_load_policy=1' : ''; 
  106. $cc_lang = ( isset( $qargs['cc_lang_pref'] ) ) ? '&cc_lang_pref=' . preg_replace( '/[^_a-z0-9-]/i', '', $qargs['cc_lang_pref'] ) : ''; 
  107.  
  108. $wmode = ( isset( $qargs['wmode'] ) && in_array( strtolower( $qargs['wmode'] ), array( 'opaque', 'window', 'transparent' ) ) ) ? $qargs['wmode'] : 'transparent'; 
  109.  
  110. $theme = ( isset( $qargs['theme'] ) && in_array( strtolower( $qargs['theme'] ), array( 'dark', 'light' ) ) ) ? '&theme=' . $qargs['theme'] : ''; 
  111.  
  112. $autoplay = ''; 
  113. /** 
  114. * Allow YouTube videos to start playing automatically. 
  115. * @module shortcodes 
  116. * @since 2.2.2 
  117. * @param bool false Enable autoplay for YouTube videos. 
  118. */ 
  119. if ( apply_filters( 'jetpack_youtube_allow_autoplay', false ) && isset( $qargs['autoplay'] ) ) 
  120. $autoplay = '&autoplay=' . (int)$qargs['autoplay']; 
  121.  
  122. $alignmentcss = 'text-align:center;'; 
  123. if ( isset( $qargs['align'] ) ) { 
  124. switch ( $qargs['align'] ) { 
  125. case 'left': 
  126. $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;"; 
  127. break; 
  128. case 'right': 
  129. $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;"; 
  130. break; 
  131.  
  132. if ( ( isset( $url['path'] ) && '/videoseries' == $url['path'] ) || isset( $qargs['list'] ) ) { 
  133. $html = "<span class='embed-youtube' style='$alignmentcss display: block;'><iframe class='youtube-player' type='text/html' width='$w' height='$h' src='" . esc_url( set_url_scheme( "http://www.youtube.com/embed/videoseries?list=$id&hl=en_US" ) ) . "' frameborder='0' allowfullscreen='true'></iframe></span>"; 
  134. } else { 
  135. $html = "<span class='embed-youtube' style='$alignmentcss display: block;'><iframe class='youtube-player' type='text/html' width='$w' height='$h' src='" . esc_url( set_url_scheme( "http://www.youtube.com/embed/$id?version=3&rel=$rel&fs=1$fmt$autohide&showsearch=$search&showinfo=$info&iv_load_policy=$iv$start$end$hd&wmode=$wmode$theme$autoplay{$cc}{$cc_lang}" ) ) . "' frameborder='0' allowfullscreen='true'></iframe></span>"; 
  136.  
  137. /** 
  138. * Filter the YouTube video HTML output. 
  139. * @module shortcodes 
  140. * @since 1.2.3 
  141. * @param string $html YouTube video HTML output. 
  142. */ 
  143. $html = apply_filters( 'video_embed_html', $html ); 
  144.  
  145. return $html;