wp_audio_shortcode

Builds the Audio shortcode output.

Description

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

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

Returns (string|void)

HTML content to display audio.

Parameters (2)

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

Options

  • src (string) => ''

    URL to the source of the audio file.

  • loop (string) => ''

    The loop attribute for the

  • autoplay (string) => ''

    The autoplay attribute for the

  • preload (string) => 'none'

    The preload attribute for the

  • class (string) => 'wp-audio-shortcode'

    The class attribute for the

array(

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

    /**
     * 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 'none'
     */
    'preload' => 'none',

    /**
     * The 'class' attribute for the `` element.
     *
     * @type string
     * @default 'wp-audio-shortcode'
     */
    'class' => 'wp-audio-shortcode'
);        

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

Usage

  1. if ( !function_exists( 'wp_audio_shortcode' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Attributes of the audio shortcode. 
  5. $attr = array( 
  6. 'src' => '', 
  7. 'loop' => '', 
  8. 'autoplay' => '', 
  9. 'preload' => 'none', 
  10. 'class' => 'wp-audio-shortcode' 
  11. ); 
  12.  
  13. // Shortcode content. 
  14. $content = ''; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wp_audio_shortcode($attr, $content); 
  18.  

Defined (1)

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

/wp-includes/media.php  
  1. function wp_audio_shortcode( $attr, $content = '' ) { 
  2. $post_id = get_post() ? get_the_ID() : 0; 
  3.  
  4. static $instance = 0; 
  5. $instance++; 
  6.  
  7. /** 
  8. * Filters the default audio shortcode output. 
  9. * If the filtered output isn't empty, it will be used instead of generating the default audio template. 
  10. * @since 3.6.0 
  11. * @param string $html Empty variable to be replaced with shortcode markup. 
  12. * @param array $attr Attributes of the shortcode. @see wp_audio_shortcode() 
  13. * @param string $content Shortcode content. 
  14. * @param int $instance Unique numeric ID of this audio shortcode instance. 
  15. */ 
  16. $override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance ); 
  17. if ( '' !== $override ) { 
  18. return $override; 
  19.  
  20. $audio = null; 
  21.  
  22. $default_types = wp_get_audio_extensions(); 
  23. $defaults_atts = array( 
  24. 'src' => '',  
  25. 'loop' => '',  
  26. 'autoplay' => '',  
  27. 'preload' => 'none',  
  28. 'class' => 'wp-audio-shortcode',  
  29. 'style' => 'width: 100%;' 
  30. ); 
  31. foreach ( $default_types as $type ) { 
  32. $defaults_atts[$type] = ''; 
  33.  
  34. $atts = shortcode_atts( $defaults_atts, $attr, 'audio' ); 
  35.  
  36. $primary = false; 
  37. if ( ! empty( $atts['src'] ) ) { 
  38. $type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); 
  39. if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) { 
  40. return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); 
  41. $primary = true; 
  42. array_unshift( $default_types, 'src' ); 
  43. } else { 
  44. foreach ( $default_types as $ext ) { 
  45. if ( ! empty( $atts[ $ext ] ) ) { 
  46. $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); 
  47. if ( strtolower( $type['ext'] ) === $ext ) { 
  48. $primary = true; 
  49.  
  50. if ( ! $primary ) { 
  51. $audios = get_attached_media( 'audio', $post_id ); 
  52. if ( empty( $audios ) ) { 
  53. return; 
  54.  
  55. $audio = reset( $audios ); 
  56. $atts['src'] = wp_get_attachment_url( $audio->ID ); 
  57. if ( empty( $atts['src'] ) ) { 
  58. return; 
  59.  
  60. array_unshift( $default_types, 'src' ); 
  61.  
  62. /** 
  63. * Filters the media library used for the audio shortcode. 
  64. * @since 3.6.0 
  65. * @param string $library Media library used for the audio shortcode. 
  66. */ 
  67. $library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ); 
  68. if ( 'mediaelement' === $library && did_action( 'init' ) ) { 
  69. wp_enqueue_style( 'wp-mediaelement' ); 
  70. wp_enqueue_script( 'wp-mediaelement' ); 
  71.  
  72. /** 
  73. * Filters the class attribute for the audio shortcode output container. 
  74. * @since 3.6.0 
  75. * @param string $class CSS class or list of space-separated classes. 
  76. */ 
  77. $atts['class'] = apply_filters( 'wp_audio_shortcode_class', $atts['class'] ); 
  78.  
  79. $html_atts = array( 
  80. 'class' => $atts['class'],  
  81. 'id' => sprintf( 'audio-%d-%d', $post_id, $instance ),  
  82. 'loop' => wp_validate_boolean( $atts['loop'] ),  
  83. 'autoplay' => wp_validate_boolean( $atts['autoplay'] ),  
  84. 'preload' => $atts['preload'],  
  85. 'style' => $atts['style'],  
  86. ); 
  87.  
  88. // These ones should just be omitted altogether if they are blank 
  89. foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) { 
  90. if ( empty( $html_atts[$a] ) ) { 
  91. unset( $html_atts[$a] ); 
  92.  
  93. $attr_strings = array(); 
  94. foreach ( $html_atts as $k => $v ) { 
  95. $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; 
  96.  
  97. $html = ''; 
  98. if ( 'mediaelement' === $library && 1 === $instance ) { 
  99. $html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n"; 
  100. $html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) ); 
  101.  
  102. $fileurl = ''; 
  103. $source = '<source type="%s" src="%s" />'; 
  104. foreach ( $default_types as $fallback ) { 
  105. if ( ! empty( $atts[ $fallback ] ) ) { 
  106. if ( empty( $fileurl ) ) { 
  107. $fileurl = $atts[ $fallback ]; 
  108. $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); 
  109. $url = add_query_arg( '_', $instance, $atts[ $fallback ] ); 
  110. $html .= sprintf( $source, $type['type'], esc_url( $url ) ); 
  111.  
  112. if ( 'mediaelement' === $library ) { 
  113. $html .= wp_mediaelement_fallback( $fileurl ); 
  114. $html .= '</audio>'; 
  115.  
  116. /** 
  117. * Filters the audio shortcode output. 
  118. * @since 3.6.0 
  119. * @param string $html Audio shortcode HTML output. 
  120. * @param array $atts Array of audio shortcode attributes. 
  121. * @param string $audio Audio file. 
  122. * @param int $post_id Post ID. 
  123. * @param string $library Media library used for the audio shortcode. 
  124. */ 
  125. return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library );