Using audio shortcode for .mp3 URLs with a query string
IT Nursery
May 16, 2022
0
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
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 1
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:
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.