WordPress Audio: Add a Download Button to mediaelement.js

So I got a call this morning asking how to download an audio file that WordPress was presenting with medialement.js. As you might have noticed, since WordPress started providing an audio player there hasn’t been an easy way to allow audio files to be downloaded as well as played. This has bothered me for a while, but this morning’s call gave me the incentive to look into it further.

My first thought was to use a custom field to store the audio file URL. Then I could display both a player and a download button based on the same URL. However, the site I was working on had years worth of audio, and I didn’t want to go through post by post and move the audio file URL out of the post editor and into a custom field. My next thought was to modify how WordPress loaded the media player and hopefully filter the output to add a download link. While this may be possible, I ended up deciding against digging into the filters in WordPress core and just used a few lines of jQuery. Here’s what did it:

// Add download links to default audio player.
jQuery( document ).ready(function($) {
	$('audio').each(function( index ) {

		var source = $(this).find('source').attr('src');

		if ( source != '' ) {
			$(this).after('<a href="' + source + '" class="audio-download" download>Download</a>');


Keep in mind you’ll need to load jQuery in order for this to run.

I just added a few lines of CSS to style the new download buttons:

a.audio-download {
	margin-top: 1em;
	background: #eee;
	padding: 0.5em 1.5em;
	border-radius: 5px;
	display: inline-block;

a.audio-download:hover {
	background: #ddd;

…and that’s it!

Posted in

18 thoughts on “WordPress Audio: Add a Download Button to mediaelement.js”

  1. Hello,
    I need help, please help me understand how i can add the same to my blog. Is it there no way that i can just copy and paste the short cut on my block , and then add the url of the audio that i wan’t to offer for downlaod and it just works?

    1. You’ll need to add the code to either your theme or a plugin and make sure that jQuery is being loaded as well. If all that is in place, then yes, the download button should show up.

  2. Many thanks for this. It’s exactly what I have been looking for.

    With a child theme, would I add the jQuery to the functions.php?

  3. Very nice. please could you explain more, how do i get it to work ? I dont know much about the wp_enqueue and you also talked about jQuery.


    1. I don’t have a screenshot of it. However, I can explain a little bit about what the code does. The first section of code simply adds an unstyled link after the media player. The second section of code turns that link into a button with a little bit of padding, a light gray background and rounded corners. I hope that helps!

  4. Thank you for posting this! I tried it today and it still works, albeit with a little modifications. I had to start the code with the following:
    jQuery( document ).ready(function($) {

    And I also tested it with video files and it does the same (just switch out ‘audio’ to ‘video’ in the code. So thanks again 🙂

    1. Thanks, Happyvisiter! I’ve updated the code example to reflect that change. I definitely should have had it that way to start with.

  5. Thanks for the code, works perfectly!

    Is there a way to integrate the button inside the player? Specifically besides the full screen button (to the left).

    I’d then use CSS to add an image.

    1. Hi Leandro, that’s a great question! I tested it out, and it looks like the player is provided by the browser. This means that it isn’t a collection of HTML elements, but rather a single element controlled by the browser. Unfortunately, that means we can’t just add our download button inside it. I believe you would need a third-party player such as Flowplayer or JW Player in order to modify the player itself.

        1. That’s probably because it’s using the wp_audio_shortcode filter. Have you tried using the wp_video_shortcode filter instead?

  6. Hi, thank you very much, I was stuck on this problem and I actually thought of using an ACF to retrieve the link manually but it was way too tedious! Thanks a lot, that’s a huge time saver!

Leave a Comment

Your email address will not be published. Required fields are marked *