返回介绍

wp_audio_shortcode()

发布于 2017-09-11 11:33:49 字数 8773 浏览 862 评论 0 收藏 0

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

Builds the Audio shortcode output.


description

This implements the functionality of the Audio Shortcode for displaying WordPress mp3s in a post.


参数

$attr

(array) (Required) Attributes of the audio shortcode.

  • 'src'
    (string) URL to the 源代码 of the audio file. Default empty.
  • 'loop'
    (string) The 'loop' attribute for the <audio> element. Default empty.
  • 'autoplay'
    (string) The 'autoplay' attribute for the <audio> element. Default empty.
  • 'preload'
    (string) The 'preload' attribute for the <audio> element. Default 'none'.
  • 'class'
    (string) The 'class' attribute for the <audio> element. Default 'wp-audio-shortcode'.
  • 'style'
    (string) The 'style' attribute for the <audio> element. Default 'width: 100%;'.

$content

(string) (Optional) Shortcode content.

Default value: ''


返回值

(string|void) HTML content to display audio.


源代码

File: wp-includes/media.php

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

	static $instance = 0;
	$instance++;

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

	$audio = null;

	$default_types = wp_get_audio_extensions();
	$defaults_atts = array(
		'src'      => '',
		'loop'     => '',
		'autoplay' => '',
		'preload'  => 'none',
		'class'    => 'wp-audio-shortcode',
		'style'    => 'width: 100%;'
	);
	foreach ( $default_types as $type ) {
		$defaults_atts[$type] = '';
	}

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

	$primary = false;
	if ( ! empty( $atts['src'] ) ) {
		$type = wp_check_filetype( $atts['src'], wp_get_mime_types() );
		if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) {
			return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) );
		}
		$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 ) {
		$audios = get_attached_media( 'audio', $post_id );
		if ( empty( $audios ) ) {
			return;
		}

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

		array_unshift( $default_types, 'src' );
	}

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

	/**
	 * Filters the class attribute for the audio shortcode output container.
	 *
	 * @since 3.6.0
	 *
	 * @param string $class CSS class or list of space-separated classes.
	 */
	$atts['class'] = apply_filters( 'wp_audio_shortcode_class', $atts['class'] );

	$html_atts = array(
		'class'    => $atts['class'],
		'id'       => sprintf( 'audio-%d-%d', $post_id, $instance ),
		'loop'     => wp_validate_boolean( $atts['loop'] ),
		'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
		'preload'  => $atts['preload'],
		'style'    => $atts['style'],
	);

	// These ones should just be omitted altogether if they are blank
	foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) {
		if ( empty( $html_atts[$a] ) ) {
			unset( $html_atts[$a] );
		}
	}

	$attr_strings = array();
	foreach ( $html_atts as $k => $v ) {
		$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
	}

	$html = '';
	if ( 'mediaelement' === $library && 1 === $instance ) {
		$html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
	}
	$html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) );

	$fileurl = '';
	$源代码 = '<源代码 type="%s" src="%s" />';
	foreach ( $default_types as $fallback ) {
		if ( ! empty( $atts[ $fallback ] ) ) {
			if ( empty( $fileurl ) ) {
				$fileurl = $atts[ $fallback ];
			}
			$type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
			$url = add_query_arg( '_', $instance, $atts[ $fallback ] );
			$html .= sprintf( $源代码, $type['type'], esc_url( $url ) );
		}
	}

	if ( 'mediaelement' === $library ) {
		$html .= wp_mediaelement_fallback( $fileurl );
	}
	$html .= '</audio>';

	/**
	 * Filters the audio shortcode output.
	 *
	 * @since 3.6.0
	 *
	 * @param string $html    Audio shortcode HTML output.
	 * @param array  $atts    Array of audio shortcode attributes.
	 * @param string $audio   Audio file.
	 * @param int    $post_id Post ID.
	 * @param string $library Media library used for the audio shortcode.
	 */
	return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library );
}

更新日志

Versiondescription
3.6.0Introduced.

相关函数

Uses

  • 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/functions.wp-scripts.php: wp_enqueue_script()
  • wp-includes/functions.php: wp_check_filetype()
  • wp-includes/functions.php: wp_get_mime_types()
  • wp-includes/functions.php: add_query_arg()
  • 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_audio_shortcode_library
  • wp-includes/media.php: wp_audio_shortcode_class
  • wp-includes/media.php: wp_audio_shortcode
  • wp-includes/media.php: wp_get_audio_extensions()
  • wp-includes/media.php: wp_mediaelement_fallback()
  • wp-includes/media.php: wp_audio_shortcode_override
  • wp-includes/post.php: wp_get_attachment_url()
  • wp-includes/post.php: get_post()
  • Show 17 more uses Hide more uses

Used By

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

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Example

    Display the HTML for an audiofile and the default media player:

    
    $attr = array(
    	'src'      => 'http://example.com/my-audiofile.mp3',
    	'loop'     => '',
    	'autoplay' => '',
    	'preload' => 'none'
    );
    echo wp_audio_shortcode( $attr );
    

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

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

发布评论

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