返回介绍

wp_video_shortcode()

发布于 2017-09-11 13:10:41 字数 7677 浏览 1115 评论 0 收藏 0

wp_video_shortcode( array $attr,  string $content = '' )

Builds the Video shortcode output.


description

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


参数

$attr

(array) (Required) Attributes of the shortcode.

  • 'src'
    (string) URL to the 源代码 of the video file. Default empty.
  • 'height'
    (int) Height of the video embed in pixels. Default 360.
  • 'width'
    (int) Width of the video embed in pixels. Default $content_width or 640.
  • 'poster'
    (string) The 'poster' attribute for the <video> element. Default empty.
  • 'loop'
    (string) The 'loop' attribute for the <video> element. Default empty.
  • 'autoplay'
    (string) The 'autoplay' attribute for the <video> element. Default empty.
  • 'preload'
    (string) The 'preload' attribute for the <video> element. Default 'metadata'.
  • 'class'
    (string) The 'class' attribute for the <video> element. Default 'wp-video-shortcode'.

$content

(string) (Optional) Shortcode content.

Default value: ''


返回值

(string|void) HTML content to display video.


源代码

File: wp-includes/media.php

function wp_video_shortcode( $attr, $content = '' ) {
	global $content_width;
	$post_id = get_post() ? get_the_ID() : 0;

	static $instance = 0;
	$instance++;

	/**
	 * Filters the default video shortcode output.
	 *
	 * If the filtered output isn't empty, it will be used instead of generating
	 * the default video template.
	 *
	 * @since 3.6.0
	 *
	 * @see wp_video_shortcode()
	 *
	 * @param string $html     Empty variable to be replaced with shortcode markup.
	 * @param array  $attr     Attributes of the video shortcode.
	 * @param string $content  Video shortcode content.
	 * @param int    $instance Unique numeric ID of this video shortcode instance.
	 */
	$override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance );
	if ( '' !== $override ) {
		return $override;
	}

	$video = null;

	$default_types = wp_get_video_extensions();
	$defaults_atts = array(
		'src'      => '',
		'poster'   => '',
		'loop'     => '',
		'autoplay' => '',
		'preload'  => 'metadata',
		'width'    => 640,
		'height'   => 360,
		'class'    => 'wp-video-shortcode',
	);

	foreach ( $default_types as $type ) {
		$defaults_atts[$type] = '';
	}

	$atts = shortcode_atts( $defaults_atts, $attr, 'video' );

	if ( is_admin() ) {
		// shrink the video so it isn't huge in the admin
		if ( $atts['width'] > $defaults_atts['width'] ) {
			$atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] );
			$atts['width'] = $defaults_atts['width'];
		}
	} else {
		// if the video is bigger than the theme
		if ( ! empty( $content_width ) && $atts['width'] > $content_width ) {
			$atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] );
			$atts['width'] = $content_width;
		}
	}

	$is_vimeo = $is_youtube = false;
	$yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
	$vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#';

	$primary = false;
	if ( ! empty( $atts['src'] ) ) {
		$is_vimeo = ( preg_match( $vimeo_pattern, $atts['src'] ) );
		$is_youtube = (  preg_match( $yt_pattern, $atts['src'] ) );
		if ( ! $is_youtube && ! $is_vimeo ) {
			$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
			if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) {
				return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
			}
		}

		if ( $is_vimeo ) {
			wp_enqueue_script( 'froogaloop' );
		}

		$primary = true;
		array_unshift( $default_types, 'src' );
	} else {
		foreach ( $default_types as $ext ) {
			if ( ! empty( $atts[ $ext ] ) ) {
				$type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() );
				if ( strtolower( $type['ext'] ) === $ext ) {
					$primary = true;
				}
			}
		}
	}

	if ( ! $primary ) {
		$videos = get_attached_media( 'video', $post_id );
		if ( empty( $videos ) ) {
			return;
		}

		$video = reset( $videos );
		$atts['src'] = wp_get_attachment_url( $video->ID );
		if ( empty( $atts['src'] ) ) {
			return;
		}

		array_unshift( $default_types, 'src' );
	}

	/**
	 * Filters the media library used for the video shortcode.
	 *
	 * @since 3.6.0
	 *
	 * @param string $library Media library used for the video shortcode.
	 */
	$library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' );
	if ( 'mediaelement' === $library && did_action( 'init' ) ) {
		wp_enqueue_style( 'wp-mediaelement' );
		wp_enqueue_script( 'wp-mediaelement' );
	}

	// Mediaelement has issues with some URL formats for Vimeo and YouTube, so
	// update the URL to prevent the ME.js player from breaking.
	if ( 'mediaelement' === $library ) {
		if ( $is_youtube ) {
			// Remove `feature` query arg and force SSL - see

更新日志

Versiondescription
3.6.0Introduced.

相关函数

Uses

  • wp-includes/http.php: wp_parse_url()
  • wp-includes/functions.php: wp_validate_boolean()
  • wp-includes/formatting.php: esc_url()
  • wp-includes/formatting.php: esc_html()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/load.php: is_admin()
  • wp-includes/functions.wp-scripts.php: wp_enqueue_script()
  • wp-includes/functions.php: absint()
  • wp-includes/functions.php: wp_check_filetype()
  • wp-includes/functions.php: wp_get_mime_types()
  • wp-includes/functions.php: remove_query_arg()
  • wp-includes/functions.php: add_query_arg()
  • wp-includes/link-template.php: set_url_scheme()
  • wp-includes/functions.wp-styles.php: wp_enqueue_style()
  • wp-includes/shortcodes.php: shortcode_atts()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/plugin.php: did_action()
  • wp-includes/post-template.php: get_the_ID()
  • wp-includes/media.php: get_attached_media()
  • wp-includes/media.php: wp_get_video_extensions()
  • wp-includes/media.php: wp_video_shortcode_override
  • wp-includes/media.php: wp_video_shortcode_library
  • wp-includes/media.php: wp_video_shortcode_class
  • wp-includes/media.php: wp_video_shortcode
  • wp-includes/media.php: wp_mediaelement_fallback()
  • wp-includes/post.php: wp_get_attachment_url()
  • wp-includes/post.php: get_post()
  • Show 22 more uses Hide more uses

Used By

  • wp-includes/widgets/class-wp-widget-media-video.php: WP_Widget_Media_Video::render_media()
  • wp-admin/includes/media.php: edit_form_image_editor()
  • wp-includes/post-template.php: prepend_attachment()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文