On PHP I usually I use these lines of code to force a download:
header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream', true, 200); header('Content-Disposition: attachment; filename="MY_FILENAME"'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); ob_clean(); ob_flush(); flush(); readfile('MY_FILE_PATH'); exit();
Now I tried to include the WordPress enviroment to it simply adding this on top of it:
require_once(dirname(__FILE__) . '/wp-load.php');
But now instead of instantly opening the browser download file dialog, the script runs for minutes (this lasts more for bigger files, it looks like the server is downloading the file) then only after this the browser shows the download file dialog where the user could clicks OK and starts the real download.
If I comment out the WordPress include it works as expected.
I have to use it for remote files, but it looks the same also on local files.
I tried various combinations of output buffering cleaning, flushing and restarting too.
Any hint on what feature of WordPress could cause this?