Using audio shortcode for .mp3 URLs with a query string

The problem:

The problem seems to be that wp_check_filetype() doesn’t recognize mp3 files with GET parameters.

Possible workarounds:

You have two options as far as I can see:

1) Override the audio shortcode with the wp_audio_shortcode_override filter.

2) or allow any audio extensions via the wp_audio_extensions filter.

Here’s an example how you can implement the latter option:

/**
 * Allow unrecognize audio sources hosted on 'bucketname.s3.amazonaws.com'.
 *
 * @see http://wordpress.stackexchange.com/a/152352/26350
 */

add_filter( 'wp_audio_shortcode_override',
    function( $html, $atts )
    {
        if( 'bucketname.s3.amazonaws.com' === parse_url( $atts['src'], PHP_URL_HOST ) )
        {
            add_filter( 'wp_audio_extensions', 'wpse_152316_wp_audio_extensions' );
        }
        return $html;
    }
, PHP_INT_MAX, 2 );

where

function wpse_152316_wp_audio_extensions( $ext )
{
    remove_filter( current_filter(), __FUNCTION__ );
    $ext[] = '';
    return $ext;
}

So just to explain what’s happening here:

Right before the wp_get_audio_extensions() call, inside the wp_audio_shortcode() function, we “hijack” the wp_audio_shortcode_override hook, to allow the empty audio file extension.
The reason for this is that the wp_check_filetype() check returns an empty string for unrecognized file extensions. We then make sure that this modification only applies when the audio source is hosted on bucketname.s3.amazonaws.com.

I hope this helps.

Leave a Comment