wp_video_shortcode

Builds the Video shortcode output.

Description

(string|void) wp_video_shortcode( (array) $attr, (string) $content = '' ); 

This implements the functionality of the Video Shortcode for displaying WordPress mp4s in a post.

Returns (string|void)

HTML content to display video.

Parameters (2)

0. $attr (array)
Attributes of the shortcode.

Options

  • src (string) => ''

    URL to the source of the video file.

  • height (int) => 360

    Height of the video embed in pixels.

  • width (int) => $content_width or 640

    Width of the video embed in pixels.

  • poster (string) => ''

    The poster attribute for the

  • loop (string) => ''

    The loop attribute for the

  • autoplay (string) => ''

    The autoplay attribute for the

  • preload (string) => 'metadata'

    The preload attribute for the

array(

    /**
     * URL to the source of the video file.
     *
     * @type string
     * @default ''
     */
    'src' => '',

    /**
     * Height of the video embed in pixels.
     *
     * @type int
     * @default 360
     */
    'height' => 360,

    /**
     * Width of the video embed in pixels.
     *
     * @type int
     * @default $content_width or 640
     */
    'width' => $content_width or 640,

    /**
     * The 'poster' attribute for the `` element.
     *
     * @type string
     * @default ''
     */
    'poster' => '',

    /**
     * The 'loop' attribute for the `` element.
     *
     * @type string
     * @default ''
     */
    'loop' => '',

    /**
     * The 'autoplay' attribute for the `` element.
     *
     * @type string
     * @default ''
     */
    'autoplay' => '',

    /**
     * The 'preload' attribute for the `` element.
     *
     * @type string
     * @default 'metadata'
     */
    'preload' => 'metadata'
);        

1. $content — Optional. (string) => ''
Shortcode content.

Usage

  1. if ( !function_exists( 'wp_video_shortcode' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Attributes of the shortcode. 
  5. $attr = array( 
  6. 'src' => '', 
  7. 'height' => 360, 
  8. 'width' => $content_width or 640, 
  9. 'poster' => '', 
  10. 'loop' => '', 
  11. 'autoplay' => '', 
  12. 'preload' => 'metadata' 
  13. ); 
  14.  
  15. // Shortcode content. 
  16. $content = ''; 
  17.  
  18. // NOTICE! Understand what this does before running. 
  19. $result = wp_video_shortcode($attr, $content); 
  20.  

Defined (1)

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

/wp-includes/media.php  
  1. function wp_video_shortcode( $attr, $content = '' ) { 
  2. global $content_width; 
  3. $post_id = get_post() ? get_the_ID() : 0; 
  4.  
  5. static $instance = 0; 
  6. $instance++; 
  7.  
  8. /** 
  9. * Filters the default video shortcode output. 
  10. * If the filtered output isn't empty, it will be used instead of generating 
  11. * the default video template. 
  12. * @since 3.6.0 
  13. * @see wp_video_shortcode() 
  14. * @param string $html Empty variable to be replaced with shortcode markup. 
  15. * @param array $attr Attributes of the video shortcode. 
  16. * @param string $content Video shortcode content. 
  17. * @param int $instance Unique numeric ID of this video shortcode instance. 
  18. */ 
  19. $override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance ); 
  20. if ( '' !== $override ) { 
  21. return $override; 
  22.  
  23. $video = null; 
  24.  
  25. $default_types = wp_get_video_extensions(); 
  26. $defaults_atts = array( 
  27. 'src' => '',  
  28. 'poster' => '',  
  29. 'loop' => '',  
  30. 'autoplay' => '',  
  31. 'preload' => 'metadata',  
  32. 'width' => 640,  
  33. 'height' => 360,  
  34. 'class' => 'wp-video-shortcode',  
  35. ); 
  36.  
  37. foreach ( $default_types as $type ) { 
  38. $defaults_atts[$type] = ''; 
  39.  
  40. $atts = shortcode_atts( $defaults_atts, $attr, 'video' ); 
  41.  
  42. if ( is_admin() ) { 
  43. // shrink the video so it isn't huge in the admin 
  44. if ( $atts['width'] > $defaults_atts['width'] ) { 
  45. $atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] ); 
  46. $atts['width'] = $defaults_atts['width']; 
  47. } else { 
  48. // if the video is bigger than the theme 
  49. if ( ! empty( $content_width ) && $atts['width'] > $content_width ) { 
  50. $atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] ); 
  51. $atts['width'] = $content_width; 
  52.  
  53. $is_vimeo = $is_youtube = false; 
  54. $yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#'; 
  55. $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#'; 
  56.  
  57. $primary = false; 
  58. if ( ! empty( $atts['src'] ) ) { 
  59. $is_vimeo = ( preg_match( $vimeo_pattern, $atts['src'] ) ); 
  60. $is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) ); 
  61. if ( ! $is_youtube && ! $is_vimeo ) { 
  62. $type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); 
  63. if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) { 
  64. return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); 
  65.  
  66. if ( $is_vimeo ) { 
  67. wp_enqueue_script( 'froogaloop' ); 
  68.  
  69. $primary = true; 
  70. array_unshift( $default_types, 'src' ); 
  71. } else { 
  72. foreach ( $default_types as $ext ) { 
  73. if ( ! empty( $atts[ $ext ] ) ) { 
  74. $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); 
  75. if ( strtolower( $type['ext'] ) === $ext ) { 
  76. $primary = true; 
  77.  
  78. if ( ! $primary ) { 
  79. $videos = get_attached_media( 'video', $post_id ); 
  80. if ( empty( $videos ) ) { 
  81. return; 
  82.  
  83. $video = reset( $videos ); 
  84. $atts['src'] = wp_get_attachment_url( $video->ID ); 
  85. if ( empty( $atts['src'] ) ) { 
  86. return; 
  87.  
  88. array_unshift( $default_types, 'src' ); 
  89.  
  90. /** 
  91. * Filters the media library used for the video shortcode. 
  92. * @since 3.6.0 
  93. * @param string $library Media library used for the video shortcode. 
  94. */ 
  95. $library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' ); 
  96. if ( 'mediaelement' === $library && did_action( 'init' ) ) { 
  97. wp_enqueue_style( 'wp-mediaelement' ); 
  98. wp_enqueue_script( 'wp-mediaelement' ); 
  99.  
  100. /** 
  101. * Filters the class attribute for the video shortcode output container. 
  102. * @since 3.6.0 
  103. * @param string $class CSS class or list of space-separated classes. 
  104. */ 
  105. $atts['class'] = apply_filters( 'wp_video_shortcode_class', $atts['class'] ); 
  106.  
  107. $html_atts = array( 
  108. 'class' => $atts['class'],  
  109. 'id' => sprintf( 'video-%d-%d', $post_id, $instance ),  
  110. 'width' => absint( $atts['width'] ),  
  111. 'height' => absint( $atts['height'] ),  
  112. 'poster' => esc_url( $atts['poster'] ),  
  113. 'loop' => wp_validate_boolean( $atts['loop'] ),  
  114. 'autoplay' => wp_validate_boolean( $atts['autoplay'] ),  
  115. 'preload' => $atts['preload'],  
  116. ); 
  117.  
  118. // These ones should just be omitted altogether if they are blank 
  119. foreach ( array( 'poster', 'loop', 'autoplay', 'preload' ) as $a ) { 
  120. if ( empty( $html_atts[$a] ) ) { 
  121. unset( $html_atts[$a] ); 
  122.  
  123. $attr_strings = array(); 
  124. foreach ( $html_atts as $k => $v ) { 
  125. $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; 
  126.  
  127. $html = ''; 
  128. if ( 'mediaelement' === $library && 1 === $instance ) { 
  129. $html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n"; 
  130. $html .= sprintf( '<video %s controls="controls">', join( ' ', $attr_strings ) ); 
  131.  
  132. $fileurl = ''; 
  133. $source = '<source type="%s" src="%s" />'; 
  134. foreach ( $default_types as $fallback ) { 
  135. if ( ! empty( $atts[ $fallback ] ) ) { 
  136. if ( empty( $fileurl ) ) { 
  137. $fileurl = $atts[ $fallback ]; 
  138. if ( 'src' === $fallback && $is_youtube ) { 
  139. $type = array( 'type' => 'video/youtube' ); 
  140. } elseif ( 'src' === $fallback && $is_vimeo ) { 
  141. $type = array( 'type' => 'video/vimeo' ); 
  142. } else { 
  143. $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); 
  144. $url = add_query_arg( '_', $instance, $atts[ $fallback ] ); 
  145. $html .= sprintf( $source, $type['type'], esc_url( $url ) ); 
  146.  
  147. if ( ! empty( $content ) ) { 
  148. if ( false !== strpos( $content, "\n" ) ) { 
  149. $content = str_replace( array( "\r\n", "\n", "\t" ), '', $content ); 
  150. $html .= trim( $content ); 
  151.  
  152. if ( 'mediaelement' === $library ) { 
  153. $html .= wp_mediaelement_fallback( $fileurl ); 
  154. $html .= '</video>'; 
  155.  
  156. $width_rule = ''; 
  157. if ( ! empty( $atts['width'] ) ) { 
  158. $width_rule = sprintf( 'width: %dpx;', $atts['width'] ); 
  159. $output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html ); 
  160.  
  161. /** 
  162. * Filters the output of the video shortcode. 
  163. * @since 3.6.0 
  164. * @param string $output Video shortcode HTML output. 
  165. * @param array $atts Array of video shortcode attributes. 
  166. * @param string $video Video file. 
  167. * @param int $post_id Post ID. 
  168. * @param string $library Media library used for the video shortcode. 
  169. */ 
  170. return apply_filters( 'wp_video_shortcode', $output, $atts, $video, $post_id, $library );