I’m trying to use the audio shortcode with an audio file with a query string on the end of the URL and it won’t work. The reason is I’d like to use expiring signed URLs with Amazon S3.
If I use a URL like
as the src in a call to wp_audio_shortcode()
it works just fine. But if I use a URL like
http://bucketname.s3.amazonaws.com/audiofile.mp3?AWSAccessKeyId=XXXXXXXXXXXX&Expires=1234567890&Signature=XXXXXXXXXXXXXXXXXX
then wp_audio_shortcode()
just spits out an HTML link instead of the audio embed and player code. In my tests it appears the shortcode requires a file extension of .mp3 be at the end of the URL. Is there a way to make the short code work with a query string on the URL?
Any other solutions to suggest?
1 Answer
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.