If you want to force a file to download in the WordPress backend? Just add the following to your theme’s functions.php
file or in another file that you include
in functions.php
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Force a file to download in the WordPress backend. */ function fwm_template_redirect() { if ($_SERVER['REQUEST_URI']=='/downloads/data.csv') { header("Content-type: application/x-msdownload",true,200); header("Content-Disposition: attachment; filename=data.csv"); header("Pragma: no-cache"); header("Expires: 0"); echo 'data'; exit(); } } add_action( 'template_redirect','fwm_template_redirect' ); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.