Created
March 23, 2016 12:56
-
-
Save sjaved87/3674d304d3fa80656506 to your computer and use it in GitHub Desktop.
In rare case, if you want to remove the query string added by WordPress at the end of mp3 files. You can use this snippet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if( !function_exists('wpmudev_remove_query_string_from_mp_url') ) : | |
add_filter('wp_audio_shortcode', 'wpmudev_remove_query_string_from_mp_url', 10, 5); | |
function wpmudev_remove_query_string_from_mp_url( $html, $atts, $audio, $post_id, $library ){ | |
$post_id = get_post() ? get_the_ID() : 0; | |
static $instance = 0; | |
$instance++; | |
/** | |
* Filter 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' | |
); | |
foreach ( $default_types as $type ) { | |
$defaults_atts[$type] = ''; | |
} | |
$atts = shortcode_atts( $defaults_atts, $atts, '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' ); | |
} | |
/** | |
* Filter 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' ); | |
} | |
/** | |
* Filter the class attribute for the audio shortcode output container. | |
* | |
* @since 3.6.0 | |
* | |
* @param string $class CSS class or list of space-separated classes. | |
*/ | |
$html_atts = array( | |
'class' => apply_filters( 'wp_audio_shortcode_class', 'wp-audio-shortcode' ), | |
'id' => sprintf( 'audio-%d-%d', $post_id, $instance ), | |
'loop' => wp_validate_boolean( $atts['loop'] ), | |
'autoplay' => wp_validate_boolean( $atts['autoplay'] ), | |
'preload' => $atts['preload'], | |
'style' => 'width: 100%; visibility: hidden;', | |
); | |
// 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 = ''; | |
$source = '<source 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 = $atts[ $fallback ] ; | |
$html .= sprintf( $source, $type['type'], esc_url( $url ) ); | |
} | |
} | |
if ( 'mediaelement' === $library ) { | |
$html .= wp_mediaelement_fallback( $fileurl ); | |
} | |
$html .= '</audio>'; | |
return $html; | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment