I'm trying to find a solution for forcing audio files to download on mobile using PHP.
I tried using the MIME type 'AddType audio/mpeg .mp3' in my .htaccess file to no effect. Direct downloads work fine on desktop but not on mobile - it always redirects to the default Wordpress player to download instead of just downloading after clicking the link.
I've searched various solutions and this is the closet I got Forcing to download a file using PHP
My files are hosted outside of my domain. This is the PHP file I'm using but it's not working.
$file_name = 'file.mp3';
$file_url = 'https://mcdn.podbean.com/mf/web/' . $file_name;
header('Content-Type: audio/mpeg');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename="my-file.mp3");
readfile($file_url);
exit;
These files are hosted with another provider on another domain. Nothing you can do will force the direct download unless they were hosted on a platform like AWS where you can create your own bucket and link it to your domain. That's costly.
Podbean suggested I change "web" in the podcast URL to "download." I used the Find and Replace All Wordpress plugin and it automatically changed them all. Just make sure you search for whatever proceeds the word you want to change so you don't replace a common word like "web" elsewhere in your database.
Always backup your database before making mass changes.
With over 800 episodes it worked perfectly after clearing the cache.